处理 Laravel 5 中的 TokenMismatchException [英] Handle TokenMismatchException in laravel 5

查看:22
本文介绍了处理 Laravel 5 中的 TokenMismatchException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 laravel 5 中处理 TokenMismatchException,这样如果令牌不匹配,它就会向用户显示一些消息而不是 TokenMismatchException 错误.

解决方案

您可以创建自定义异常渲染

a> 在 AppExceptionsHandler 类中(在 /app/Exceptions/Handler.php 文件中).

例如,要在出现 TokenMismatchException 错误时呈现不同的视图,您可以将 render 方法更改为如下所示:

/*** 将异常渲染到 HTTP 响应中.** @param IlluminateHttpRequest $request* @param Exception $e* @return IlluminateHttpResponse*/公共函数渲染($request,异常 $e){if ($e instanceof IlluminateSessionTokenMismatchException) {return response()->view('errors.custom', [], 500);}return parent::render($request, $e);}

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.

You can create a custom exception render in the AppExceptionsHandler class (in the /app/Exceptions/Handler.php file).

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  IlluminateHttpRequest  $request
 * @param  Exception  $e
 * @return IlluminateHttpResponse
 */
public function render($request, Exception $e)
{
    if ($e instanceof IlluminateSessionTokenMismatchException) {
        return response()->view('errors.custom', [], 500);
    }
    return parent::render($request, $e);
}

这篇关于处理 Laravel 5 中的 TokenMismatchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆