PhalconPHP 无法在另一个控制器中获取会话值

PhalconPHP not able to get session values in another controller

我正在使用 PhalconPHP 并在登录后在会话中存储值,如下所示。

$this->session->start();
    $this->session->set('auth', array(
        'dealer_id' => $dealers->getDealerId(),
        'username'  => $dealers->getUserName(),
        'language_id' => $dealers->getLanguageId(),
        'dealername'  => $dealername,
    ));

    session_write_close();

它在会话中设置值,如果我尝试打印,即使在那之后也是如此

print_r($this->session->get('auth'))

它 returns

Array
(
    [dealer_id] => 78
    [username] => swiftmailcomm
    [language_id] => 1
    [dealername] => Swiftmail Communication
)

但是当我尝试在某些操作中使用 ControllerBase 中的 $this->session->get('auth') 获取此会话值时,它不会 return 任何东西。好像毁了。

Module.Php

public function registerServices(\Phalcon\DiInterface $di)
{
    $di->set('dispatcher',function(){

        $eventsManager = new EventsManager;

        /**
         * Check if the user is allowed to access certain action using the SecurityPlugin
         */
        $eventsManager->attach('dispatch:beforeDispatch', new SecurityPlugin);


        /**
         * Handle exceptions and not-found exceptions using NotFoundPlugin
         */
        $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin);

        $dispatcher = new MvcDispatcher;
        $dispatcher->setEventsManager($eventsManager);
        $dispatcher = new \Phalcon\Mvc\Dispatcher();
        $dispatcher->setDefaultNamespace("NBBD_SkyWebTech\Dealer\Controllers\");
        return $dispatcher;
    });
    /**
     * Read configuration
     */
    $config = include __DIR__ . "/config/config.php";

    //Register Volt as a service
    $di['view'] = function () use ($config) {

        $view = new View();
        $view->setViewsDir($config->application->viewsDir);
        //activating volt engine
        $view->registerEngines(array(
            ".volt" => 'voltService'
        ));
        return $view;
    };
    $di->set('voltService', function($view, $di) use ($config) {

        $volt = new Volt($view, $di);
        $volt->setOptions(array(
            "compiledPath" => $config->application->voltCacheDir,
            'compiledSeparator' => '_',
            "compiledExtension" => ".compiled"
        ));
        return $volt;
    });

     /*
     * Setting up the view component

    $di['view'] = function () use ($config) {
        $view = new View();
        $view->setViewsDir($config->application->viewsDir);
        //activating volt engine
        $view->registerEngines(array(
            ".phtml" => 'voltService'
        ));
        return $view;
    }; */
    //Set the views cache service
    $di->set('viewCache', function() use ($config) {

        // Cache the files for 1hour using a Output frontend
        $frontCache = new Output(array(
            "lifetime" => 3600
        ));
        //Cache data for one day by default
        $cache = new File($frontCache, array(
            "cacheDir" => $config->application->cacheDir
        ));

        return $cache;
    });

    //Model Cache
    $di->set('modelsCache', function() use ($config) {

        //Cache data for one day by default
        $frontCache = new \Phalcon\Cache\Frontend\Data(array(
            "lifetime" => 3600
        ));

        //Cache data for one day by default
        $cache = new File($frontCache, array(
            "cacheDir" => $config->application->cacheDir
        ));

        return $cache;
    });

    //Set up the flash service
    $di->set('flash', function() {
        return new \Phalcon\Flash\Session(array(
            'error'   => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice'  => 'alert alert-info',
        ));
    });

    /**
     * Start the session the first time some component request the session service
     */
    $di->set('session', function () {
        $session = new SessionAdapter();
        $session->start();

        return $session;
    });

}

在您的会话服务声明中,您使用了依赖注入器方法"set"。我相信您需要使用 "setShared" 因为这会使会话服务充当单例,这将使您在其中设置的任何内容都可用于其他控制器。

我有这个确切的设置,它对我有用。

我认为使用 set 方法会导致它每次都创建新会话,而不是拉取包含您的数据的现有会话。

    $di->setShared('session', function () {
        $session = new SessionAdapter();

        $session->start();

        return $session;
    });