We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Session works only in same Action

I created a docker container with last phalcon version in docker and now sessions don't works anymore, just works in the same action, in other action in same controller or not don't works.

I created a new phalcon project to test and i get the same problem, someone get the same problem? Anybody can test for confirm this error?

*** Container Info ***
Nginx last stable version
PHP5-FPM last stable version
Phalcon last stable version
Ubuntu14.4


17.5k

Whta does your services.php look like?

<?php
/**
 * Services are globally registered in this file
 *
 * @var \Phalcon\Config $config
 */

use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Direct as Flash;

/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->setShared('url', function () use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
});

/**
 * Setting up the view component
 */
$di->setShared('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
});

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->setShared('db', function () use ($config) {
    $dbConfig = $config->database->toArray();
    $adapter = $dbConfig['adapter'];
    unset($dbConfig['adapter']);

    $class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;

    return new $class($dbConfig);
});

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->setShared('modelsMetadata', function () {
    return new MetaDataAdapter();
});

/**
 * Register the session flash service with the Twitter Bootstrap classes
 */
$di->set('flash', function () {
    return new Flash(array(
        'error'   => 'alert alert-danger',
        'success' => 'alert alert-success',
        'notice'  => 'alert alert-info',
        'warning' => 'alert alert-warning'
    ));
});

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

    return $session;
});


17.5k

Looks good. The biggest difference I see between yours and mine are flash and modelsMetadata... Maybe comment those out and see if session works? Looking at the file, I have no other ides.

I realized tests here now and realized that session variables are persisted to the other controller actions only if I put it in an initialize method in the controller, otherwise if I create them within an action they are only persisted within that action, if I go to another action they are no longer displayed.

//This way works in others controller actions
//but not in another controllers
public function initialize()
    {
      $this->session->set('name', 'MarcosJr');
      $this->persistent->name = 'MJ';
    }

And even putting them in the initialize method of a controller if I go to another controller they are no longer displayed, I find it strange, I thought the session variables should be global, and before so it worked, I do not know coz no longer works this way, you have any idea?

Looks good. The biggest difference I see between yours and mine are flash and modelsMetadata... Maybe comment those out and see if session works? Looking at the file, I have no other ides.

I continued conducting tests here, this time created a container installing PHALCON / legacy, and the session worked perfectly, as I suspected is a problem with this latest version of Phalcon, so that to install this latest version I had never had this kind of problem before, somehow this latest estable version of Phalcon leaves epnas session variables in the action scopo where it was created, or do not know why but it's a serious bug ...

Anyone else could test to confirm this?



17.5k

What are you doing to retrieve the value in the other actions/controllers?

$this->session->get('name'); should be what you're calling.

I don't use $this->persistent anywhere, so not sure about that one.

I did both ways just to test if any of them work, but none worked on the latest version of PHALCON only in legacy, the legacy both work perfectly.

What are you doing to retrieve the value in the other actions/controllers?

$this->session->get('name'); should be what you're calling.

I don't use $this->persistent anywhere, so not sure about that one.