Mohamed Mehanny
Mohamed Mehanny

Reputation: 307

Laravel 5.2 - adding page title to URL

I have a project like amazon based on Laravel 5.2.

I need to show title of product and category in URL of page which preview with only ID.

For example:

ID: 2
Name: Apple iPhone 6s

The URL: www.mysite.com/showproduct/2

I need this to be: www.mysite.com/showproduct/2-Apple-IPhone-6s

Upvotes: 1

Views: 1356

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

If you take a look to Laravel 5.2 documentation especially Route Parameters part, you will notice a red box there say :

Note: Route parameters cannot contain the - character. Use an underscore (_) instead.

So your route could be:

Route::get('showproduct/{title}', 'Controller@action');

But instead of 2-Apple-IPhone-6s it should be 2_Apple_IPhone_6s, IMO better if you could use it as:

Route::get('showproduct/{id}/{title?}', 'Controller@action');

Hope this helps.

Upvotes: 1

Mojtaba
Mojtaba

Reputation: 5004

Try this: https://github.com/cviebrock/eloquent-sluggable

Don't make the wheel again ;)

Upvotes: 0

Related Questions