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

Global var in VIEW

I want set global var in VIEW for authorized users:

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

    $di->set('view', function() use ($config) {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir( APP_PATH . $config->phalcon->viewsDir );
        $view->registerEngines(array(
            ".phtml" => 'voltService'
        ));
        $session = new Phalcon\Session\Adapter\Files();
        $auth = $session->get('auth');
        if( $auth )
            $view->setVar("auth" , true);
        return $view;
    });

this code not work, because in $di->set('view') section i can not get $session->auth, but if i use construction:

$di->set('view', function() use ($config, $di) {
        ...
        $session = $di->get('session');
        $auth = $session->get('auth');
        //var_dump($auth);
        if( $auth )
            $view->setVar("auth" , true);
        ...
}

$di->set('session') call twice and i have notice "A session had already been started - ignoring session_start()" And i think what this not a right method. Thx for help.

-$session = $di->get('session');
+$session = $di->getShared('session');


13.8k

Спасибо :)