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

时间:2019-01-08 13:36:53

标签: php session phalcon phalconphp

我正在使用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'))

返回

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

但是,当我尝试使用$this->session->get('auth')中的ControllerBase获取此会话值时,它不会返回任何内容。它似乎已被摧毁。

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;
    });

}

1 个答案:

答案 0 :(得分:1)

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

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

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

@NgModule({
...
providers: [
    { provide: HTTP_INTERCEPTORS, useClass: HttpErrorFilter, multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: HttpFilter, multi: true },
    ...
  ],
...
})