Routing 'pretty' URLs to items with specific IDs

Submitted by Brunty - 9 years ago

Ever wanted to route pretty SEO urls to a controller with a specific ID without having to query the DB every time to match the URL to the page? On a number of projects previously, I've created cached versions of route files so that we can save all the URLs for a site and cut down on DB queries to work out what item we're viewing. Create another routes file that you write to whenever an entry is saved. You may end up with a lot of entries in the file, as each route will be for an individual item. Pass the ID to your controller action via the route like so:

Route::get('/some-pretty-seo-url', function() {
    $id = 2;
    return App::make('PagesController')->showPage($id);
});