【问题标题】:PhalconPHP not able to get session values in another controllerPhalconPHP 无法在另一个控制器中获取会话值
【发布时间】:2019-01-08 13:36:53
【问题描述】:

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

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

模块.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;
    });

}

【问题讨论】:

  • 能否请您包括会话声明
  • 我现在没有时间给你一个完整的答案,但你的问题是session_write_close()。删除这些电话。如果它解决了问题,我将发布详细答案以供将来参考
  • @UlugToprak 我做了,但仍然是同样的问题.. 请发布完整的答案。谢谢
  • 你不需要再调用$this->session->start();start()是在DI取回服务的时候调用的。
  • @UlugToprak 我已将其从代码中删除。问题依然存在...

标签: php session phalcon


【解决方案1】:

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

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

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

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

        $session->start();

        return $session;
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 2018-05-17
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多