Reputation: 3289
I have several categories that I would like named routes for and they are all using the same controller action, so I've grouped them and did what I thought would work, but it doesn't. If I only have one route, it works, but breaks when I add others. I am still new to Laravel, so it doesn't surprise me--routing like this seems a bit dirty, but a lot of posts I've found advocate many hard-coded routes.
web.php
Route::group(['prefix' => 'categories', 'as' => 'categories.'], function () {
Route::get('/', 'CategoryController@index')->name('categories');
Route::get('/{category}', 'CategoryController@show')->where('category', 'amusement-rides')->name('amusement-rides');
Route::get('/{category}', 'CategoryController@show')->where('category', 'arts-and-crafts')->name('arts-and-crafts');
Route::get('/{category}', 'CategoryController@show')->where('category', 'carnival-booths')->name('carnival-booths');
Route::get('/{category}', 'CategoryController@show')->where('category', 'carnival-games')->name('carnival-games');
Route::get('/{category}', 'CategoryController@show')->where('category', 'concession-machine-rental')->name('concession-machine-rental');
Route::get('/{category}', 'CategoryController@show')->where('category', 'equipment-rentals')->name('equipment-rentals');
Route::get('/{category}', 'CategoryController@show')->where('category', 'events')->name('events');
});
Upvotes: 1
Views: 571
Reputation: 15296
Your web.php file should like.
Web.php
Route::group(['prefix' => 'categories', 'as' => 'categories.'], function () {
Route::get('/', 'CategoryController@index')->name('categories');
Route::get('/{category}', 'CategoryController@show')->name('show');
});
In your browser the URLs called be like.
URL
http://baseurl/categories/amusement-rides
http://baseurl/categories/arts-and-crafts
http://baseurl/categories/carnival-booths
http://baseurl/categories/carnival-games
http://baseurl/categories/concession-machine-rental
http://baseurl/categories/equipment-rentals
http://baseurl/categories/equipment-rentals
finally, If you want to access category name in controller then you may access by using parameter like.
Controller
public function show($categories){
echo $categories; //here you'll get amusement-rides, arts-and-crafts, carnival-booths, carnival-games
}
Upvotes: 2