duanqian9503 2015-11-12 18:18
浏览 283
已采纳

Laravel在中间件中使用route参数

Couldn't find anything that specifically matches my situation. I have a route group defined as:

Route::group(['prefix' => 'api/v1/{access_token}'], function(){
    ...
}

The above group has several resource routes inside. I am trying to create a custom middleware that will validate the access_token parameter and return a 400 response if the parameter is not valid. I would like to be able to so something like this in my controllers:

class ProductController extends Controller {

    /**
     * Instantiate a new ProductController
     */
    public function __construct()
    {
        $this->middleware('verifyAccessToken');
    }
    ...
}

My question is not "how do I define custom middleware", but rather, how can I gain access to the access_token parameter from within the handle function of my custom middleware?

EDIT: While the question suggested as a duplicate is similar and has an answer, that answer seems to be outdated and/or unsatisfactory for what I am trying to accomplish.

  • 写回答

3条回答 默认 最新

  • duanfuxing2212 2015-11-12 18:23
    关注

    You can just access it from your $request object using the magic __get method like this:

    public function handle($request, Closure $next)
    {
        $token = $request->access_token;
        // Do something with $token
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?