Mohamed Mehanny Mohamed Mehanny - 4 months ago 56
PHP Question

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

Answer

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.

Comments