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 error

Am setting up a session in a controller function using the following;

      $di->setShared('session', function() {
        $session = new Phalcon\Session\Adapter\Files();
        $session->start();
        return $session;
    });

    but when I run var_dump($session->getId());exit; within the functionI get the following errors;
    1.Notice: Undefined variable: di in 
    2.Fatal error: Call to a member function setShared() on a non-object in 

My objective is to start a session with will be active in the whole application.

Thanks for the help



3.4k
Accepted
answer

If you are in a controller function you have to use $this->getDi()->setShared(...)

If you are in a controller function you have to use $this->getDi()->setShared(...)

Thanks alot. This has sorted my errors but now when I dump $this->session null value is returned

edited Nov '14

For me it works if I set it in controller method and dump it afterwards.

Maybe because I used $di = new FactoryDefault() in my services.php config file.