如何在laravel 4中为url友好创建自定义路由?

时间:2014-11-14 07:53:44

标签: php laravel laravel-4 routes

我正在使用laravel 4路由为我的项目创建友好的URL。我想用这种格式获取网址: http://www.mywebsite.com/post/post-slug-content-123。 post-slug-content是我的slug,123是我的帖子ID。 我的路线:

Route::get('post/{name}-{id}','postController@detail'); 

它不起作用,因为laravel检测“post”是我的{name}变量,而“slug”是我的{id}变量,但我想“post-slug-content”是{name}而123是{id} 。任何想法???

1 个答案:

答案 0 :(得分:1)

使用-无效,-会考虑在post-slug-content-123

如果您的网址是 http://www.mywebsite.com/post/post_slug_content-123

http://www.mywebsite.com/post/postSlugContent-123 尝试使用

Route::get('post/{name}-{id}','postController@detail');  

因为您有多个-,它会将其视为单独的网址

或者您可以尝试

1:Route::get('post/{nameId}','postController@detail');您的姓名和身份证件都存在,您可以在postController中将其分开。

2:Route::get('post/{name}/{id}','postController@detail');这样可以正常使用

或使用stackoverflow种类id followed by name

3:Route::get('post/{id}/{name}','postController@detail');

你也可以使用@itachi

所说的 http://www.mywebsite.com/post/post-slug-content_123

4:Route::get('post/{name}_{id}','postController@detail');

传递参数Here的进一步参考。