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条)

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?