Laravel Resource Controller Slug

时间秒杀一切 提交于 2019-12-11 03:49:44

问题


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 !


回答1:


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);
}


来源:https://stackoverflow.com/questions/23080420/laravel-resource-controller-slug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!