Ad

Create Dynamic Routes In Laravel

- 1 answer

I'm working on ecommerce website, Stuck in nav bar, I have created routes something like this:

Route::get('/category/{slug}', 'Site\[email protected]')->name('category.show');

In slug i pass slug of Product,

but i want to change something like this

 Route::get('/{slug}/{slug}', 'Site\[email protected]')->name('category.show');

i want to remove category prefix and pass main category slug as a first parameter and sub category if it exist as a second parameter otherwise it will be empty.

One more thing i am using TypiCMS, for creating Nestable menu and it is working will i have to modify that also to work with the dynamic route.

Ad

Answer

Sorry I don't know anything about your cms framework but in Laravel you cant use the same name for two bindings in your route, they must each be unique.

Route::get('/{categoyrySlug}/{subcategorySlug}', 'Site\[email protected]')->name('category.show');

And in Site\CategoryController you should be able to use :

public function show($categorySlug, $subcategorySlug){
    ...
}

and then handle them accodingly.

Ad
source: stackoverflow.com
Ad