【问题标题】:Flash data not being written to session闪存数据未写入会话
【发布时间】:2020-05-25 17:55:01
【问题描述】:

我正在使用 Laravel 7.12.0,但我的 flash 会话数据似乎根本无法使用

request()->session()->flash('status', 'Task was successful!');

return redirect()->route('home')->with('status', "Task was successful");

我正在使用 Debugbar 以及 dd'ing 页面来检查会话数据。

我也知道正在使用网络中间件。

session()->put() 仍然有效,所以我知道将数据放入会话并没有完全损坏,但闪存数据似乎根本不起作用。这是一个全新的安装,所以我正在努力看看可能出了什么问题?

Web.php

Auth::routes();


Route::get('/', 'HomeController@index')->name('home');

Route::group(['middleware' => ['auth']], function () {
    Route::resource('polls', 'PollController');
    Route::post('polls/vote', 'PollController@vote')->name('polls.vote');
});

Route::resource('api/polls', 'API\PollController');

RouteServiceProvider.php的一部分

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }

kernel.php 中的 Web 中间件组

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{

    public function index()
    {
        dd(request()->session()->all());
    }
}

重定向后来自我的 HomeController@index 的会话中的内容。

  "_token" => "pbZJgfC6XNG2eTqlcGADm68NqhjOHI16rWe4U1bt"
  "_previous" => array:1 [▼
    "url" => "http://127.0.0.1:8000"
  ]
  "_flash" => array:2 [▼
    "old" => []
    "new" => []
  ]
  "url" => []
  "login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 1
]

谢谢

【问题讨论】:

  • 为什么你说它不起作用?
  • 在您的home 路由中,您如何处理会话?您是否在任何地方显示它,您是否执行另一个重定向或发生了什么?
  • @Qirel 在 HomeController@index 中我只有dd(request()-&gt;session()-&gt;all());,只是为了查看它有哪些会话数据。我将编辑我的帖子以显示我收到的回复。
  • withErrors() 有效吗?

标签: php laravel laravel-7 inertiajs


【解决方案1】:

似乎这与托管环境有关。使用http://127.0.0.1:8000 时,我能够在本地计算机上重现该问题。切换到http://localhost:8000 后,会话开始按预期持续使用 ->flash 和 ->with。

【讨论】:

  • 谢谢,但问题仍未解决。我仍然得到相同的结果。
  • 我能看到你的控制器吗?
  • 谢谢...您在哪个控制器中调用上述重定向?
  • 一个名为PollController的控制器。目前,控制器方法仅包含重定向,没有其他内容。它是从PollController@store 调用的。在我的网络路线中,它位于资源中,例如Route::resource('polls', 'PollController');
  • @Graeme ... 更新了我的答案。让我们看看这是否适合你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
相关资源
最近更新 更多