Support the ongoing development of Laravel.io →
Configuration Requests Architecture
Last updated 1 year ago.
0
Route::get('/books/{id}/{name}', 'HomeController@showBook');
0

hey thanks for answering .. but this way the url in tha address bar will look like : www.site.com/books/1/title-of-book which is not what I really want ....

I want to keep the same Route i defined with only the id as a parameter and show the name of the book in the address bar so it looks like : www.site.com/books/title-of-book

0

Unfortunately you can't have your cake and eat it too. If you want to keep only the ID as a parameter, then it is not possible to include the title as a parameter.

You could probably make the ID look a little bit more obscure by using a route like '/books/{title}.{id}' which would give you a URL like www.mysite.com/books/The-Giver.1/ but either way you will need both in there.

0

Ok I got it guys .. thanks for your answers even if I knew it before ;)

0

Not sure if its true, for example many websites have url like mock.com/user/nickname, without any integers or mock.com/2015/08/google-less-apps/ where is only date, but no post ids :)

Last updated 8 years ago.
0

Sure it's possible to have a string there but the string has to be something unique to identify the correct dataset. And you cannot call www.example.com/books/harry-potter and then magically get id 15 there since that information has to come from somewhere. :)

With a middleware it might be possible.

0

As Ftiersch mentioned you need to have unique identify. Like Wordpress post name is convert to "slug" and stored in database with separate column with unique. You can do the same thing with laravel.

Laravel even have a helper function for it. http://laravel.com/docs/5.1/helpers#method-str-slug

so, when you add the new book, you store book name and book slug. book slug needs to be unique. And your url should be like

Route::get('/books/{slug}', 'HomeController@showBook');


// in method

Book::where('slug', '=', $slug)->first(); 

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.