处理laravel中的TokenMismatchException 5 [英] Handle TokenMismatchException in laravel 5
问题描述
我需要在laravel 5中处理 TokenMismatchException
,如果令牌不匹配,则会向用户显示一些消息,而不是 TokenMismatchException
错误。
I need to handle TokenMismatchException
in laravel 5 such a way that if token does not match it will show some message to user instead of TokenMismatchException
error.
推荐答案
您可以创建一个自定义 App \Exceptions\Handler 类中的 c $ c>中的/docs/5.1/errors#render-method\"> exception render /app/Exceptions/Handler.php 文件)
You can create a custom exception render in the App\Exceptions\Handler
class (in the /app/Exceptions/Handler.php
file).
例如,为呈现不同的视图时, TokenMismatchException
错误,您可以将 render
方法更改为这样:
For example, to render a different view when for the TokenMismatchException
error, you can change the render
method to something like this:
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof \Illuminate\Session\TokenMismatchException) {
return response()->view('errors.custom', [], 500);
}
return parent::render($request, $e);
}
这篇关于处理laravel中的TokenMismatchException 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!