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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了