0
votes

I've 2 routes in my web.php

1) Route::get('/{url}', 'MenuController@menu');

which provide url :

  • /menu

2) Route::get('/{name}', 'HomeSlideviewController@index')->name('promotiondetail');

which provide url :

  • /menu (different page but same name in route 1)
  • /food

I want to use 2 route if route = same name I want to use route 1 if route 1 dont have url It will use route 2 . In web.php is their anyway to do something like

if(Route::get('/{url}', 'MenuController@menu')) is null use

`Route::get('/{name}', 'HomeSlideviewController@index')->name('promotiondetail');`

now in my web.php I do this

 Route::get('/{url}', 'MenuController@menu');
 Route::get('/{name}', 'HomeSlideviewController@index')->name('promotiondetail');

when I go /food It will go page not found.

UPDATE

In my controller I try this

try {
    // if find url
}
} catch (\Exception $e) {
    //if not find url
    return redirect()->route('promotiondetail', $url);
}   

and It return Error redirected you too many times

UPDATE 3

$url = food

2

2 Answers

3
votes

Your problem is that when you use

Route::get('/{url}', 'MenuController@menu');
Route::get('/{name}', 'HomeSlideviewController@index')->name('promotiondetail');

you are having the same request because {url} or {name} are optional parameters and what happens is that it will always match the first case. The best solution for you can be using this part of code:

Route::get('/menu', 'MenuController@menu');
Route::get('/{name}', 'HomeSlideviewController@index')->name('promotiondetail');

You should always have the one with only optional parameters last, because otherwise it will always be executed first because it will be matching. And what you should remember is that using /{name} it will match anything, it is like a variable and can contain a number also it can be a string, for instance a url might be domain/{anything}. If you use /name it will match only if you are having domain/name as request.

You might want to read Laravel routing for more information about routing.

1
votes

The problem you have is that both routes are essentially the same, /{something}.

You have a couple of solutions.
Firstly, sort out your routes, make them slightly different so they don't match each other and correct the order.
For example;

Route::get('/promo/{name}', 'HomeSlideviewController@index')->name('promotiondetail');
Route::get('/{url}', 'MenuController@menu')->name('menu');

Another solution that may work for you is to place the promotiondetail route first, and do a check in that for the same name, if not then redirect to the other controller. So in your index function of HomeSlideviewController, try something like;

public function index($name) {
    if ($name !== 'whatever you want it not to be') {
        return redirect()->route('menu);
    }
    // continue
}