2014-10-07 186 views
1

我正在尝试在laravel中创建一组路由。前两个很简单。Laravel中的通配符URL路由

/加载首页
/12345通过ResultController加载12345的结果,我用{result}/完成。

我想要的第三条路线是/ 12345/foo/bar/baz,它最终将执行第二个呈现文件的控制器。基本上/ foo/bar/baz代表文件位置,因此它可以是任何深度级别。我想将它作为单个值传递给控制器​​。我尝试了以下路线简单地测试它的工作:

Route::get('/', function() 
{ 
    return View::make('home.main'); 
}); 
Route::get('{result}/', '[email protected]'); 
Route::get('{result}/(.*)', function() { 
    return 'Huzzah!'; 
}); 

目前,要低于{}导致任何路径/仍导致404例如:

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

回答

3

您可能尝试这样的事情,但可能不是一个很好的解决方案:

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。把它放在所有路线的底部。

+1

工作。谢谢。 – fishpen0 2014-10-07 02:59:47

+0

不客气:-) – 2014-10-07 03:02:31