dpxyfa4718 2014-04-28 16:23
浏览 63
已采纳

如何在Laravel控制器方法中放置参数不是强制性的?

I would like to know how to put a parameter in a Laravel controller method not obligatory, as I've got a route like this (I'm using the ? to say it's not obligatory):

Route::get('/account/student/create/{student_id?}', array(
    'as' => 'account-student-create',
    'uses' => 'StudentController@getCreate'
));

I've got in my controller:

getCreate($student_id){...}

Put if don't fill a parameter in the route I've got a warning message:

Missing argument 1 for StudentController::getCreate()

Thanks for helping!

Jean

  • 写回答

2条回答 默认 最新

  • dongpang1232 2014-04-28 16:27
    关注

    Add a default value to your controller method declaration:

    getCreate($student_id=null){...}
    

    That way when the method is called without a value, $student_id will be set to null.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?