Laravel 4命名路由中的可选参数

时间:2014-02-14 18:54:20

标签: php laravel routing optional-parameters

我有这个带有可选参数的路线:

Route::get('{id}/results/subject/{subject_id}/{step_id?}', array('as' =>'test', 'uses' => '\Controllers\TestController@Show'));

我在TestController中得到了这个

function Show($id, $subject_id, $step_id){
//Some stuff
}

我想将默认值归因于我的可选step_id参数,就像here一样。如果我没有归属于默认值,我的控制器会出现缺少的参数错误。

我试过

Route::get('{id}/results/subject/{subject_id}/{step_id?}', array('as' =>'test', 'uses' => '\Controllers\TestController@Show', function($step_id = '3'){return $step_id});

Route::get('{id}/results/subject/{subject_id}/{step_id?}',function($step_id = '3'){return $step_id}, array('as' =>'test', 'uses' => '\Controllers\TestController@Show'));

但两者都不起作用。

1 个答案:

答案 0 :(得分:1)

只需在函数中设置默认值即可。

function ShowFactorRat($id, $subject_id, $step_id="default value here"){
//Some stuff
}