dp518158 2019-05-07 19:54
浏览 122

控制器方法中的参数顺序

Suppose we have the following route in Laravel:

Route::get('/blog/{id}/{slug}', ['as' => 'blog', 'uses' => 'Front\PostController@single']);

Where the <kbd>{id}</kbd> and the <kbd>{slug}</kbd> are variables and will be passed as arguments to single() method of PostController class as below:

public function single($id, $slug) {}

But you can also pass the Request object as well, like so:

public function single(Request $request, $id, $slug) {}

or even:

public function single($id, $slug, Request $request) {}

and the Laravel still can handle them fine... looks like the order of arguments does not metter or there is some special technique applied?

  • 写回答

2条回答 默认 最新

  • duanhuilao0787 2019-05-07 20:02
    关注

    The order of the arguments is not important except if you use a default value in those arguments (available since PHP 5.6+), like this:

    function something($foo, $bar=true) { ... }
    

    Which should be the last ones in the arguments.

    Please check the official PHP docs about "use of default parameters in functions":

    The default value must be a constant expression, not (for example) a variable, a class member or a function call.

    Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100