Laravel Dynamic Routing for Pages module Like WordPress

I am hoping that someone can help me out with dynamic routing for urls that can have multiple segments. I've been doing some searching all over the web, but nothing I find helps me out with my specific situation.

A little background ... several years ago, I developed a CMS package for custom client websites that was built on CodeIgniter. This CMS package has several modules (Pages, Blog, Calendar, Inquiries, etc). For the Pages module, I was caching the routes to a "custom routes" config file that associated the full route for the page (including parent, grandparent, etc) with the ID of the page. I did this so that I didn't have to do a database lookup to find the page to display.

I am currently working on rebuilding this CMS package using Laravel (while I'm learning Laravel). I need to figure out the routing situation before I can move on with my Pages module in the new version of the package.

I know that I can do something like ...

Coed:-

// routes.php
Route::get('{slug}', ['uses' => 'PageController@view']);

// PageController.php
class PageController extends Controller
{
    public function view($slug)
    {
        // do a query to get the page by the slug
        // display the view
    }
}


And this would work if I didn't allow nested pages, but I do. And I only enforce uniqueness of the slug based on the parent. So there could be more than one page with a slug of fargo ...

locations/fargo
staff/fargo
As with the package that I built using CodeIgniter, I would like to be able to avoid extra database lookups to find the correct page to display.

My initial thought was to create a config file that would have the dynamic routes like I did with the old version of the system. The routes will only change at specific times (when page is created, when slug is modified, when parent is changed), so "caching" them would work great. But I'm still new to Laravel, so I'm not sure what the best way to go about this would be.

Is it possible to pass a specific value to a controller via a route ... a value that isn't in the URL? Maybe something like ...

Code:-

Route::get('about/locations/fargo', ['uses' => 'PageController@view', 'with' => ['id' => 123']]);

Or am I just stuck in a CodeIgniter mindset and over-complicating things?

Any help or direction regarding this would be greatly appreciated.

Thanks!

No comments

Note: only a member of this blog may post a comment.