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

Plugin

Hello, I have basic security plugin but i cant start him. Where Im wrong ?!

use Phalcon\Mvc\User\Plugin;
use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
class Security extends Plugin
{
    public function beforeDispatch(Event $event, Dispatcher $dispatcher)
    {        
        print_r('hello');exit;
        if ($dispatcher->getControllerName() != 'index' && empty($this->session->get('auth'))) {
            $this->session->destroy();
            return $this->response->redirect(\Modules\Admin\Models\Enom\IndexEnom::INDEX_URL);
        }
    }

}


6.6k
Accepted
answer

Have you registered your plugin in the dispatcher of your application?

<?php

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

    //Obtain the standard eventsManager from the DI
    $eventsManager = $di->getShared('eventsManager');

    //Instantiate the Security plugin
    $security = new Security($di);

    //Listen for events produced in the dispatcher using the Security plugin
    $eventsManager->attach('dispatch', $security);

    $dispatcher = new Phalcon\Mvc\Dispatcher();

    //Bind the EventsManager to the Dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

Check this:

https://docs.phalcon.io/en/latest/reference/tutorial-invo.html#events-management

https://forum.phalcon.io/discussion/510/using-acl-in-phalcon-1-1-0