【发布时间】:2015-09-15 04:20:45
【问题描述】:
目前我正在做 laravel 项目
在页面上看到这个错误让我很恼火。
即使我已经注意到这一点,我的用户也不会看到这个异常。
但预防胜于治疗!
那么有什么方法可以在 laravel 中处理这个异常。
【问题讨论】:
标签: laravel exception exception-handling laravel-5 token
目前我正在做 laravel 项目
在页面上看到这个错误让我很恼火。
即使我已经注意到这一点,我的用户也不会看到这个异常。
但预防胜于治疗!
那么有什么方法可以在 laravel 中处理这个异常。
【问题讨论】:
标签: laravel exception exception-handling laravel-5 token
如果您不需要 CSRF 保护,您可以在
中禁用中间件app/Http/Kernel.php
如果您稍后将应用程序环境设置为生产环境,用户无论如何都不会看到错误消息。
【讨论】:
在render() 方法中的Exception\Handler 类中,通过
if ($e instanceof \Illuminate\Session\TokenMismatchException) {
// Your code
}
并呈现您的错误页面或使用此异常执行任何其他操作。
【讨论】:
要处理此异常,请转到 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();
}
【讨论】: