简体   繁体   English

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

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

I am using laravel 4 routing for create friendly url for my project. 我正在使用laravel 4路由为我的项目创建友好的URL。 I want to get url with this format: http://www.mywebsite.com/post/post-slug-content-123 . 我想使用以下格式的网址: http : //www.mywebsite.com/post/post-slug-content-123 post-slug-content is my slug, and 123 is my post id. post-slug-content是我的子弹,123是我的帖子ID。 my route: 我的路线:

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

It not work because laravel detect "post" is my {name} variable, and "slug" is my {id} variable, but i want "post-slug-content" is {name} and 123 is {id}. 它不起作用,因为laravel检测到“ post”是我的{name}变量,而“ slug”是我的{id}变量,但是我希望“ post-slug-content”是{name},而123是{id}。 Any idea ??? 任何想法 ???

using - wont work and which - will it consider breaking in post-slug-content-123 使用-不会工作,并且会-考虑将其插入post-slug-content-123

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

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

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

because you have multiple - and it will consider it as a separate URL 因为您有多个-它会将其视为一个单独的URL

Or you may try 或者您可以尝试

1: Route::get('post/{nameId}','postController@detail'); 1: Route::get('post/{nameId}','postController@detail'); where both your name and id is present, further you could break them in your postController . 您的姓名和ID都存在的地方,还可以在postController

2: Route::get('post/{name}/{id}','postController@detail'); 2: Route::get('post/{name}/{id}','postController@detail'); this will work fine 这会很好

or use stackoverflow kind id followed by name 或使用stackoverflow种类id followed by name

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

you can also use http://www.mywebsite.com/post/post-slug-content_123 as said by @itachi 你也可以使用http://www.mywebsite.com/post/post-slug-content_123由@itachi说

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

Further reference for passing parameters Here . 用于传递参数还参考在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM