简体   繁体   中英

Laravel 4 optional paramaters in named routes

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.

Just set the default in your function like so.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM