duanjingwei7239 2017-04-07 13:16
浏览 18
已采纳

以只读模式启动会话

My problem is the following:

I have a page which runs a series of AJAX calls e.g.

$.ajax({ url: "/user/post1" });
$.ajax({ url: "/user/post2" });
$.ajax({ url: "/user/post3" });
...
$.ajax({ url: "/user/postN" }); 

(I've determined the ids beforehand)

I've also got:

$.ajax({ url: "/user/modifysessiondata" }); 

Now the problem is that I need to start the session in each and every request (to get the current user) and in addition "/user/modifysessiondata" also needs to modify session data.

However I've noticed that if I just execute all AJAX requests at once it's possible that if a /user/postX request starts before /user/modifysessiondata request and ends after that then it will save the session data as it was when the request was called and therefore will overwrite the session data that was set by the 2nd call.

My question is:

Is it possible to flag the session started in a specific request as "Read only"? Would I need to create a new custom StartSession middleware for this?

Am I just doing something completely ridiculous?

Note: To clarify I'm talking about the Laravel session. I am using a redis backed session storage.

  • 写回答

1条回答 默认 最新

  • dscrn1974 2017-04-10 10:08
    关注

    In case it helps anyone this is what I ended up doing:

    In my RouteServiceProvider.php boot() method:

    Route::group([
        'middleware' => 'readonly',
        'namespace' => $this->namespace,
    ], function ($router) {
        Route::get("user/{post}","UserController@getPost");
    });
    

    In my Kernel.php:

    'readonly' => [            
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \App\Http\Middleware\StartReadOnlySession::class,            
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,   
            'bindings',
     ]
    

    This was basically copied from the web route group with the exception of StartSession swapped for StartReadOnlySession

    Here's my StartReadOnlySession code (the punchline of the issue):

    use Illuminate\Session\Middleware\StartSession;
    class StartReadOnlySession extends StartSession {
        public function terminate($request, $response) { /* Does nothing */ }
    }
    

    This this starts the session but does not save it when the request terminates. Everything else behaves identically.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写