简体   繁体   English

Laravel中的通配符URL路由

[英]Wildcard URL routing in Laravel

I am attempting to create a set of routes in laravel. 我正在尝试在laravel中创建一组路由。 The first two are simple. 前两个很简单。

/ loads home /装回家
/12345 loads a result for 12345 via ResultController, which I accomplished with {result}/ . / 12345通过ResultController加载12345的结果,我用{result}/

The third route I would like is /12345/foo/bar/baz, which eventually will execute a second controller that presents files. 我想要的第三条路径是/ 12345 / foo / bar / baz,它最终将执行第二个显示文件的控制器。 Basically /foo/bar/baz represents the file location, so it could be any level of depth. 基本上,/ foo / bar / baz表示文件位置,因此它可以是任意深度。 I would like to pass it to the controller as a single value. 我想将其作为单个值传递给控制器​​。 I tried the below route to simply test that it would work: 我尝试了以下方法来简单地测试它是否可以工作:

Route::get('/', function()
{
    return View::make('home.main');
});
Route::get('{result}/', 'ResultController@showResult');
Route::get('{result}/(.*)', function() {
    return 'Huzzah!';
});

Currently, going to any path below {result}/ is still resulting in a 404. For example: 当前,转到{result} /以下的任何路径仍会产生404。例如:

/12345/foo -> Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

You may try something like this but probably not a very good solution: 您可以尝试这样的操作,但可能不是一个很好的解决方案:

Other route declaration
Route::get('')

// At the bottom
Route::get('{result}/{any?}', function($result, $any = null) {

    // $any is optional
    if($any) {
        $paramsArray = explode('/', $any);
        // Use $paramsArray array for other parameters
    }

})->where('any', '(.*)');

Be careful, it can catch any URL that matches with this. 请注意,它会捕获与此匹配的任何URL Put this at the bottom of all routes. 将此放在所有路线的底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM