Answer the question
In order to leave comments, you need to log in
How to organize branching in a route in Laravel?
Menu sections, brands and products are stored in tables. Tables have SLUG fields.
Head-on in routes.php it looks like this:
// select the 1st element uri
$url_path = parse_url(Request::url(), PHP_URL_PATH);
$uri_parts = explode('/', trim($url_path, ' /'));
$module = array_shift($uri_parts);
// Check if the table contains a MENU
if( \App\Menu::where('slug', '=', $module)->first(['id'])) {
// Display products
Route::get(' {module}/{page?}', ['uses'=>'[email protected]', 'as'=>$module])->where('page','^page-([2-9]| [1-9]{1}[0-9]+)\z');
// Display a specific product
Route::get('{module}/{good}', '[email protected]');
}
This processing is probably not quite right ...
Answer the question
In order to leave comments, you need to log in
Of course, not quite correct, your router turned out on your knee.
What is the problem with making 3 separate routes for menus, brands, products?
And then they will not intersect, and it will not be necessary to do this mess with the definition of where the user has entered.
/menu/{slug}
/brand/{slug}
/product/{slug}
And look for a specific position by slug in the desired controller/model, or you don’t even need to search, but use the ready-made trait https://github.com/cviebrock /eloquent-sluggable
or even out of the box https://laravel.com/docs/5.6/routing#route-model-b...
If it works, it's correct. But not pretty.
1. For pagination, there is https://laravel.com/docs/5.6/pagination. So {page} is superfluous here. And in general - /brand/2 - 2 should be a brand on not a page number.
2. I understand that brand|product should be in the menu table? Routes can be registered in a cycle through the menu table: not registered means not found. something like
foreach(\App\Menu::all() as $menu){
Route::get($menu->module_slug, $menu->module_slug.'[email protected]');
Route::get($menu->module_slug.'/{slug}', $menu->module_slug.'[email protected]_slug');
}
// хотя если пунктов меню не слишком много лучше сделать как предложено выше
...
// module_slug = 'Brand'
class BrandController{
... index(){}
.... by_slug(\App\Brand $slug){}
}
+ use https://laravel.com/docs/5.6/routing#route-model-b...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question