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

  • 写回答

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。