【问题标题】:Laravel 4 optional paramaters in named routesLaravel 4 命名路由中的可选参数
【发布时间】:2014-02-14 18:54:19
【问题描述】:

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

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'));

但两者都不起作用。

【问题讨论】:

    标签: php laravel routing optional-parameters


    【解决方案1】:

    像这样在你的函数中设置默认值。

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

    【讨论】:

      猜你喜欢
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多