Laravel dynamic routes

matuuz

i have table in my database that contains all my products categories, for example, accesories, books, toys, whatever.. i need to make routes in base of those categories (i show some of those categories in the home page and i made links with the localhost/public/CATEGORY_NAME url) so, this is my routes.php:

Route::get('/', 'HomeController@index');

what i think i gotta do is catch the 1st parameter in the url and route it to a specific controller for example ProductController@index, i'm new to laravel so i don't know how can i do that, any ideas? thanks

i've read in another post i can use Route::any with regular expressions or something like that but that "any" includes the root index am i right?

Alley Shairu

You can handle this stuff using route parameter which basically means passing an argument to the controller method.

Route::get('{name}','CategoryController@show');

Then in CategoryController your method show will have to accept an parameter.

public function show($name)
{
    //find category by name
}

If you are placing the above route at the top of your routes.php it will catch all routes with /{anything} so make sure you place it below others.

Or you can add an category word to the URL, but it depends on your requirement. Something like this

Route::get('category/{name}','CategoryController@show');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related