php 单个 Route::get() 中的多个路由调用 Laravel 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17489492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:59:31  来源:igfitidea点击:

multiple routes in single Route::get() call Laravel 4

phplaravellaravel-4

提问by AndrewMcLagan

When defining a route in Laravel 4 is it possible to define multiple URI paths within the same route?

在 Laravel 4 中定义路由时,是否可以在同一路由中定义多个 URI 路径?

presently i do the following:

目前我执行以下操作:

Route::get('/', 'DashboardController@index');
Route::get('/dashboard', array('as' => 'dashboard', 'uses' => 'v1\DashboardController@index'));

but this defeats my purpose, i would like to do something like

但这违背了我的目的,我想做类似的事情

Route::get('/, /dashboard', array('as' => 'dashboard', 'uses' => 'DashboardController@index'));

采纳答案by Markus Hofmann

If I understand your question right I'd say:

如果我理解你的问题,我会说:

Use Route Prefixing: http://laravel.com/docs/routing#route-prefixing

使用路由前缀http: //laravel.com/docs/routing#route-prefixing

Or (Optional) Route Parameters: http://laravel.com/docs/routing#route-parameters

(可选)路由参数http: //laravel.com/docs/routing#route-parameters

So for example:

例如:

Route::group(array('prefix' => '/'), function() { Route::get('dashboard', 'DashboardController@index'); });

OR

或者

Route::get('/{dashboard?}', array('as' => 'dashboard', 'uses' => 'DashboardController@index'));

回答by graemec

I believe you need to use an optional parameter with a regular expression:

我相信您需要使用带有正则表达式的可选参数:

Route::get('/{name}', array(
     'as' => 'dashboard', 
     'uses' => 'DashboardController@index')
    )->where('name', '(dashboard)?');

*Assuming you want to route to the same controller which is not entirely clear from the question.

*假设您想路由到同一个控制器,但问题中并不完全清楚。

*The current accepted answer matches everything not just /OR /dashboard.

*当前接受的答案匹配所有内容,而不仅仅是/OR /dashboard

回答by Oluwatobi Samuel Omisakin

I find it interesting for curiosity sake to attempt to solve this question posted by @Alexas a comment under @graemec's answer to post a solution that works:

出于好奇,我发现尝试解决@Alex发布的这个问题作为@graemec发布有效解决方案的答案的评论很有趣

Route::get('/{name}', [
    'as' => 'dashboard', 
    'uses' => 'DashboardController@index'
  ]
)->where('name', 'home|dashboard|'); //add as many as possible separated by |

Because the second argument of where()expects regular expressions so we can assign it to match exactlyany of those separated by |so my initial thought of proposing a whereIn()into Laravel route is resolved by this solution.

因为第二个参数where()期望正则表达式,所以我们可以将它分配为完全匹配任何分隔的那些,|所以我最初提出whereIn()进入 Laravel 路由的想法通过这个解决方案解决。

PS:This example is tested on Laravel 5.4.30

PS:此示例在 Laravel 5.4.30 上测试

Hope someone finds it useful

希望有人觉得它有用