在Laravel中间件中显示请求方法(GET,POST,..)

时间:2018-04-12 08:15:51

标签: php laravel-5.2 laravel-middleware

我在Laravel中有一个中间件类,我想获取用于记录信息的操作名称(GET,POST,DELETE,PUT,...)。我有以下代码:

public function handle($request, Closure $next)
{
    $api_key = $request->headers->get('x-api-key');
    if($api_key!=$this->auth_key){
        return $this->response->unauthorize(
            "You're not authorize to access. Make sure that you're passing your api Key"
        );
    }
    return $next($request);
}

我有这句话$request->route();这可能有所帮助,但我不知道该方法。

1 个答案:

答案 0 :(得分:1)

use Illuminate\Routing\Route;

private $route;

public __construct(Route $route) {
  $this->route = $route;
}

public function handle($request, Closure $next)
{
    $action = $this->route->getMethods(); // return array

    $api_key = $request->headers->get('x-api-key');
    if($api_key!=$this->auth_key){
        return $this->response->unauthorize(
            "You're not authorize to access. Make sure that you're passing your api Key"
        );
    }
    return $next($request);
}