duanrong5167 2014-02-14 18:54
浏览 25
已采纳

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

I have this route with an optional parameters:

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

and I got this in my TestController

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

I want to attribute a default value to my optional step_id parameter just like here. If I don't attribute a default value, I got a missing parameter error for my controller.

I've tried

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

and

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

but both are not working.

  • 写回答

1条回答 默认 最新

  • donglv9116 2014-02-14 18:56
    关注

    Just set the default in your function like so.

    function ShowFactorRat($id, $subject_id, $step_id="default value here"){
    //Some stuff
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?