0
votes

I am using laravel 4 routing for create friendly url for my project. I want to get url with this format: http://www.mywebsite.com/post/post-slug-content-123 . post-slug-content is my slug, and 123 is my post 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}. Any idea ???

1
Can you show postController::detail() - Nabil Kadimi
Why do you need the slug AND the id for the post? Either one or the other should be enough to identify your post. If you really need both, you need to separate them with a slash: Route::get('post/{name}/{id}','postController@detail'); - Adrenaxus
If i use slash, it work fine, but i want to keep both for SEO. Maybe i will try slash :D - Tran Dinh Phuc

1 Answers

1
votes

using - wont work and which - will it consider breaking in post-slug-content-123

if your url is http://www.mywebsite.com/post/post_slug_content-123

or http://www.mywebsite.com/post/postSlugContent-123 try using

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

because you have multiple - and it will consider it as a separate URL

Or you may try

1: Route::get('post/{nameId}','postController@detail'); where both your name and id is present, further you could break them in your postController.

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

or use stackoverflow kind id followed by name

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

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

Further reference for passing parameters Here.