Search code examples
phpsessionphalcon

PhalconPHP not able to get session values in another controller


I am using PhalconPHP and storing values in session after login something like below.

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

    session_write_close();

which set the values in session, even just after that if I try printing

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

it returns

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

But when I try to get this session values using $this->session->get('auth') in ControllerBase in some action it doesn't return anything. It seems to be destroyed.

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

}

Solution

  • In your declaration of the session service you use the dependency injector method "set". I believe you need to use "setShared" as this makes the session service act as a singleton which will make anything you set in it available to other controllers.

    I have this exact setup and it works for me.

    I think using the set method is causing it to create new session each time rather than pulling the existing one that contains your data.

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