dongpiao1983 2018-04-12 00:15
浏览 203
已采纳

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

I have a middleware class in Laravel and I wanted to get the action name like (GET, POST, DELETE, PUT,...) for logging the information. I have below code:

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);
}

I have this line $request->route(); that may help but I don't know about the method.

  • 写回答

1条回答 默认 最新

  • douchiwan1503 2018-04-12 00:34
    关注
    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部