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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function