【问题标题】:How to handle TokenMismatch exception in laravel 5如何在 laravel 5 中处理 TokenMismatch 异常
【发布时间】:2015-09-15 04:20:45
【问题描述】:

目前我正在做 laravel 项目

在页面上看到这个错误让我很恼火。

即使我已经注意到这一点,我的用户也不会看到这个异常。

但预防胜于治疗!

那么有什么方法可以在 laravel 中处理这个异常。

【问题讨论】:

    标签: laravel exception exception-handling laravel-5 token


    【解决方案1】:

    如果您不需要 CSRF 保护,您可以在

    中禁用中间件

    app/Http/Kernel.php

    如果您稍后将应用程序环境设置为生产环境,用户无论如何都不会看到错误消息。

    【讨论】:

      【解决方案2】:

      render() 方法中的Exception\Handler 类中,通过

      if ($e instanceof \Illuminate\Session\TokenMismatchException) {
         // Your code
      }
      

      并呈现您的错误页面或使用此异常执行任何其他操作。

      【讨论】:

        【解决方案3】:

        要处理此异常,请转到 your_laravel_folder/vendor/compiled.php 并找到这个 ::

        public function handle($request, Closure $next)
        {
           if ($this->isReading($request) || $this->tokensMatch($request)) {
               return $this->addCookieToResponse($request, $next($request));
           }
                throw new TokenMismatchException();
        }
        

        并注释 throw new TokenMismatchException();做你想做的就是例子

        public function handle($request, Closure $next)
        {
           if ($this->isReading($request) || $this->tokensMatch($request)) {
              return $this->addCookieToResponse($request, $next($request));
           }
            abort(404);
            //throw new TokenMismatchException();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-18
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-03
          • 2016-11-22
          • 2018-11-30
          相关资源
          最近更新 更多