十月 CMS 会话中断?

October CMS sessions are breaking?

我一直在尝试在 October CMS 的插件中创建一个中间件,它从输入中获取一个值并将其存储在会话中以定期显示在模板中。

中间件函数:

  public function handle($request, Closure $next)
  {
      session()->put('foo', input('foo'));
      logger('StartSession: foo: ', [session('foo')]);
      return $next($request);
  }

正在plugin.php

中注册中间件
    public function register()
    {
    $this->app->make('Illuminate\Contracts\Http\Kernel')->prependMiddleware('October\Demo\Middleware\StartSession');
    }

插件中访问session的方法

    public function registerMarkupTags()
    {
       return [
           'functions' => [
               'session' => [Session::class, 'get']
            ]
       ];
    }

演示主题中的用法

<h1>{{ session('foo') }}</h1>

这适用于第一个 运行。如果我把 foo 作为查询字符串 foo 显示在页面上。但是,如果我将查询字符串更改为 bar,那么 运行 之后,foo 会继续显示在页面上。

这是一个全新安装的 October 实例中的问题示例

https://github.com/reed-josh/october-session-issue

嗯,可能你的中间件是在会话初始化之前执行的,不确定

you can add your session data after all middleware executed

class StartSession
{
  public function handle($request, Closure $next)
  {
    $response = $next($request);

    // if we do not pass data hold old value 
    // do not override it with null
    if(input('foo')) {
        session()->put('foo', input('foo'));
    }

    logger('StartSession: foo: ', [session('foo')]);
    return $response;
  }
}

Like this

但要确保会话是如何工作的你使用获取参数添加会话它不会直接反映它将反映在你的下一个请求中。

我还注意到您没有为输入添加条件,最好添加它,否则如果没有传递参数并且您的会话数据被不必要地覆盖,它将设置 null

如有疑问请评论。