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.

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

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services