1

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?

1 Answer 1

5

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.