首页 > 解决方案 > Laravel Flash 消息和重定向无法正常工作

问题描述

我正在使用 laravel5.8"laracasts/flash": "^3.0"

在我的应用程序中,除了这段非常具体的代码之外,所有重定向和 Flash 消息都运行良好。

/* Controller */

public function show( Test $test) {
    $test->checkPermission();
    ...
}

/* Model */

public function checkPermission()
{
    flash()->warning('You can not have access to this.');
    return redirect( route('home' ) )->send();                    //Notice the send()
}

如果我将此代码与->send()(我以前从未使用过的)一起使用,我会很好地重定向到主页但没有闪烁消息。

如果我删除->send(),我会收到 flash 消息,但我没有被重定向。

我还尝试删除flash()和使用redirect()->with(). 然后会话包含消息我被重定向。但我想使用flash()或至少了解为什么它不适用于这个特定的用例。

标签: laravelsessionredirectlaravel-5laravel-5.8

解决方案


控制器应该返回重定向,而不是检查权限。尝试返回控制器中返回的检查权限。


推荐阅读