swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 786 forks source link

控制器无法获取SessionMiddleware初始化过后的session store #386

Closed jqhph closed 6 years ago

jqhph commented 6 years ago
Q A
Bug report? yes/no
Feature request? yes/no
Swoft version 1.0.7
Swoole version x.y.z (by php --ri swoole)
PHP version x.y.z (by php -v)
Runtime environment Win10/Mac/CentOS 7/Ubuntu/Docker etc.

Details

如下代码所示,sessionstore实例化后只是保存在当前中间件和sessionManager中,而sessionManager在bean容器中注册的是非单例模式,在业务代码里面无法获取调用

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $this->config = $this->sessionManager->getConfig();
        $this->sessionHandled = true;

        $isSessionAvailable = $request instanceof Request && $this->sessionConfigured();

        // If a session driver has been configured, we will need to start the session here
        // so that the data is ready for an application. Note that the Laravel sessions
        // do not make use of PHP "native" sessions in any way since they are crappy.
        if ($isSessionAvailable) {
            $this->sessionManager->setSession($session = $this->startSession($request));
            // TODO move collect garbage to timer
            $this->collectGarbage($session);
        }

        $response = $handler->handle($request);

        // Again, if the session has been configured we will need to close out the session
        // so that the attributes may be persisted to some storage medium. We will also
        // add the session identifier cookie to the application response headers now.
        if ($isSessionAvailable) {
            $this->storeCurrentUrl($request, $session);

            $response = $this->addCookieToResponse($request, $response, $session);

            // Save session after response
            // TODO use coroutine task to save the session data
            $this->save();
        }

        return $response;
    }
huangzhhui commented 6 years ago

@jqhph 使用方法请参考 https://github.com/swoft-cloud/swoft/blob/master/app/Controllers/SessionController.php