phpooplaravelcontrollers

Laravel Resource Controller Slug


I'm trying to make my own simple CMS for laravel. I can add pages now and show them the only problem I have is the page url.

Route:

Route::group(array('prefix' => 'admin', 'before' => 'auth.admin'), function()
{
Route::resource('pages', 'App\Controllers\Admin\PagesController');
}

Now this is my link: http://domain.com/admin/pages/2 to access my page with id 2, in my database I have a slug column how can I change the link to the slug that belongs to id 2 so I get the following link:

http://domain.com/slug

Hope you can help me !


Solution

  • The route you need to set up is

    Route::get('{slug}', 'App\Controllers\Admin\PagesController@show');
    

    Then in your controller

    public function show($slug)
    {
        $page = Page::where('slug', '=', $slug)->get();
        return View::make('your.template')->withPage($page);
    }