duanqie5741
2017-11-02 08:15
浏览 57
已采纳

在web.php laravel中切换路由

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

图片转代码服务由CSDN问答提供 功能建议

我的web.php中有2条路线

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

提供url:

  • / menu

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

    提供url:

    • / menu(不同的页面,但相同) 路线中的名称1)
    • / food

      如果路线=同名,我想使用2路线我想使用路线1 如果路由1没有url它将使用路由2。 在web.php中他们无论如何都要做类似

        if(Route :: get('/ {url}','MenuController @ menu'))为null使用\  n 
      `Route :: get('/ {name}','HomeSlideviewController @ index') - &gt; name('promotiondetail');`
         
       
       

      现在在我的web.php中我这样做

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

      当我去/食物时它会找不到页面 。

      UPDATE

      在我的控制器中,我试试这个

        尝试{
       // if find url 
      } 
      } catch(\ Exception $ e){
       //如果找不到url 
      则返回redirect() - &gt; route('promotiondetail',$ url);  
      } 
         
       
       

      并返回错误重定向太多次

      更新3 < / p>

      $ url = food

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongyilu3143 2017-11-02 09:11
    最佳回答

    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条)

相关推荐 更多相似问题