如何在 laravel 4 中创建自定义路由以进行 url 友好


How to create custome route in laravel 4 for url friendly?

我正在使用 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}。任何想法???

使用-不起作用,它会考虑在哪个-中断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');  

因为您有多个-并且会将其视为单独的 URL

或者你可以试试

1:Route::get('post/{nameId}','postController@detail');您的姓名和ID都存在,您可以在postController中进一步破坏它们。

2:Route::get('post/{name}/{id}','postController@detail');这将正常工作

或使用堆栈溢出类型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');

有关传递参数的进一步参考 此处.