Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉威尔5号威瑟罗()对威瑟罗()的比赛_Php_Laravel 5.4 - Fatal编程技术网

Php 拉威尔5号威瑟罗()对威瑟罗()的比赛

Php 拉威尔5号威瑟罗()对威瑟罗()的比赛,php,laravel-5.4,Php,Laravel 5.4,我可能在做一些非常愚蠢的事情。意外地产生了一段代码,我似乎无法理解它是如何工作的 我正在尝试自定义未经验证的用户重定向到登录页面。因此,当用户试图访问受保护的页面时,laravel会将用户重定向到登录页面。在这种情况下,我试图显示Javascript toastr消息 我所做的是在App\Exceptions\Handler.php类中自定义unauthenticated方法,如下所示: protected function unauthenticated($request, Authentic

我可能在做一些非常愚蠢的事情。意外地产生了一段代码,我似乎无法理解它是如何工作的

我正在尝试自定义未经验证的用户重定向到登录页面。因此,当用户试图访问受保护的页面时,laravel会将用户重定向到登录页面。在这种情况下,我试图显示Javascript toastr消息

我所做的是在
App\Exceptions\Handler.php
类中自定义
unauthenticated
方法,如下所示:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }
    //dd($request);
    return redirect()->guest('login')->withError('You are not logged in or Your session has expired');
}
<script>
 @if(Session::has('error'))

       toastr.error("{{Session::get('error')}}");
 @endif
 </script>
请注意,我错过了
withError
函数中的
s
。在我的刀片文件中,我显示toastr通知,如下所示:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }
    //dd($request);
    return redirect()->guest('login')->withError('You are not logged in or Your session has expired');
}
<script>
 @if(Session::has('error'))

       toastr.error("{{Session::get('error')}}");
 @endif
 </script>

@if(Session::has('error'))
错误(“{Session::get('error')}”);
@恩迪夫
因此,每当用户因为尚未登录而被重定向到登录页面时,就会显示一条Java脚本toastr消息。但是,我无法理解
Session('error')
值是如何由
withError
函数填充的。我只能发现Laravel具有
withError
功能,而不是
withError


在过去的几个小时里,我一直试图排除故障,但运气不佳。在这里寻求一些指导。谢谢顺便说一句,我对Laravel很陌生。

这是一个“神奇”的方法,因为对前缀为
的方法的任何调用都将作为闪存数据添加到会话中。因此
withError()
将在键
error
下添加闪存数据
withErrors()
将在键
errors
下添加闪存数据
withSuccess()
将在
success
键下添加闪存数据;等等。

确实如此!!谢谢。我终于可以安心睡觉了。接受了你的回答。很抱歉从拉威尔搬到Yii有一段时间了,所以不要长时间检查stackoverflow!!如果我的回答解决了你的问题,你能接受吗?