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

Auth session not pass to security plugin beforeDispatch function

Hi All,

I have to deploy project to centos server and i have problem with security plugin.

In class SecurityPlugin and function beforeDispatch i have to call auth session that set from Login function like this.

Login function

$this->session->set('auth', array( 'account_no' => $userInfo['partnerAccountNo'], 'login_name' => $userInfo['firstName'], 'last_name' => $userInfo['lastName'], ));

And show that in beforeDispatch, but it can not get session

beforeDispatch function

$auth = $this->session->get('auth'); var_dump($auth);
if (!$auth){ $role = 'Guests'; } else { $role = 'Users'; }

the $auth variable show null after var_dump.

Pls help me, thanks



2.1k

Anyone pls help me



12.2k
Accepted
answer

You did initialize session within $di?


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

    return $session;
});

Also, you did attach the plugin to the eventsManager and eventsManager to dispatcher (and you did create a method beforeDispatch in SecurityPlugin.php)?


        $di->set('dispatcher', function() use ($config) {

            $eventsManager = new EventsManager();
            $eventsManager->attach('dispatch:beforeDispatch', new SecurityPlugin);

            $dispatcher = new Dispatcher();
            $dispatcher->setEventsManager($eventsManager);

            $dispatcher->setDefaultNamespace($config->application->modules->admin->defaultNamespace);

            return $dispatcher;
        });

Did you try with the beforeExecuteRoute() event instead of beforeDispatch?

Also did you register the namespace for the plugin in the loader (and then used that namespace)?


$loader->registerNamespaces(
    array(
        //...
        'Plugins'   => $config->application->pluginsDir,
        //'...
    )
);