本文介绍了Laravel中的通配符URL路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在laravel中创建一组路由.前两个很简单.

I am attempting to create a set of routes in laravel. The first two are simple.

/加载首页
/12345通过ResultController加载12345的结果,这是用 {result}/完成的.

我想要的第三条路径是/12345/foo/bar/baz,它最终将执行第二个显示文件的控制器.基本上,/foo/bar/baz表示文件位置,因此它可以是任意深度.我想将其作为单个值传递给控制器​​.我尝试了以下方法来简单地测试它是否可以工作:

The third route I would like is /12345/foo/bar/baz, which eventually will execute a second controller that presents files. Basically /foo/bar/baz represents the file location, so it could be any level of depth. 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!';
});

当前,转到{result}/以下的任何路径仍会产生404.例如:

Currently, going to any path below {result}/ is still resulting in a 404. For example:

/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', '(.*)');

请注意,它会捕获与此匹配的任何 URL .将此放在所有路线的底部.

Be careful, it can catch any URL that matches with this. Put this at the bottom of all routes.

这篇关于Laravel中的通配符URL路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:19