dongzhiji0814 2015-11-18 04:16
浏览 26
已采纳

在没有外墙的流明中验证路线参数

Hope you guys can help me, searched long and hard but cannot find my answer.

I am building an API in Lumen, I have a GET route that points to a controller, defined as:

public function mymethod(Request $request, $param1, $param2, $param3)

With POST/PUT routes, I use the included validator as such:

$validator = app('validator')->make($request->all(), $rules, $messages, $customAttributes );

In the GET route outlined above, dd( $request->all() ) returns an empty array (as expected I guess), is there any way to get an array of all the route parameters that have been passed to the method so that I can validate them without writing ugly and specific validation rules?

I could do it manually like:

$parameters = [ 'param1' => $param1, 'param2' => $param2, ... ]

but I was wondering if there was a Laravel way, looks like the Input::all() method might do what I need, but I would prefer to avoid using facades.

Thank you for any help!

  • 写回答

1条回答 默认 最新

  • dongpin1850 2015-11-18 07:58
    关注

    The Input facade is just a facade to your $request variable, so Input::all() and $request->all() are basically the same thing.

    The thing to keep in mind, though, is that you're looking for route parameters, not query string parameters. The request only knows about query string parameters, so you really need to drill down to the route object to get the route parameters.

    After looking through the Lumen code, I believe you'll want something like this. The code can be chained together, I just broke it into separate lines to be able to comment each step.

    // get the Illuminate\Http\Request object for the current request
    $request = app('request');
    
    // get the route information associated with the current request
    $route = $request->route();
    
    // route parameters are the 3rd item in the route info array
    $params = $route[2];
    
    // chained code (just for completeness)
    $params = app('request')->route()[2];
    
    // validate
    $validator = app('validator')->make($params, $rules, $messages, $customAttributes);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入