如何通过laravel中的特殊信息来管理一个特殊的错误 [英] how to manage a special error with special message in laravel

查看:140
本文介绍了如何通过laravel中的特殊信息来管理一个特殊的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 5.4。我需要处理一些错误。
想象用户登录并打开两个窗口(他的个人资料)。当用户在窗口中单击注销时,我们在另一个窗口中仍然有注销按钮,通过点击该按钮,laravel将显示csrf_token错误页面。



我的注销不是ajax,它的提交表单到 / logout
我该如何处理这个错误与特殊的消息或重定向到家里没有错误从注销控制器? (不是所有的 csrf_token 错误,只是从该控制器)。



注销表单:



我将通过点击使用jquery的注销按钮提交此表单:

 < form id =logout-formaction =/ logoutmethod =POSTstyle =display:none;> 
< input type =hiddenname =_ token:value =token>
< / form>

控制器中的注销方法:

  public function logout(Request $ request)
{
$ this-> guard() - > logout();
$ request-> session() - > flush();
$ request-> session() - > regenerate();
return redirect('/');
}


解决方案

\\Exceptions\Handler.php
使用新的有效的 CSRF 令牌将用户返回到表单,因此页面将被刷新,注销按钮将不存在。

  public function render($ request,Exception $ exception)
{
if($ exception instanceof TokenMismatchException)
{
return redirect()
- > back()
- > with('your msg');
}
return parent :: render($ request,$ exception);
}

这样看起来,页面被刷新 / p>


不要用Get替换POST。它不会安全和标准。



I'm using laravel 5.4. and I need handle some error. imagine user logged in and opened two windows (his profile). When user click on logout in a window, we have still logout button in another window, and by clicking on that, laravel will show csrf_token error page.

My logout in not ajax and its with submitting a form to /logout how can I handle this error with special message or redirect to home without error from logout controller? (not all of csrf_token errors, just from that controller).

logout form :

i will submit this form by clicking on logout button using jquery:

<form id="logout-form" action="/logout" method="POST" style="display: none;">
    <input type="hidden" name="_token" :value="token">
</form>

And the logout method in controller :

public function logout(Request $request)
{
    $this->guard()->logout();
    $request->session()->flush();
    $request->session()->regenerate();
    return redirect('/');
}

解决方案

in App\Exceptions\Handler.php Return the user to the form with a new valid CSRF token, so the page will refreshed and logout button will not exist.

public function render($request, Exception $exception)
{ 
   if($exception instanceof TokenMismatchException)
   { 
      return redirect()
               ->back()
               ->with('your msg');
   }
   return parent::render($request, $exception); 
}

this looking like, page was refreshed.

Don't Replace POST with Get. It will not Safe And Standard.

这篇关于如何通过laravel中的特殊信息来管理一个特殊的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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