Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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);
        }
    }

}


4.3k
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:

http://docs.phalconphp.com/en/latest/reference/tutorial-invo.html#events-management

http://forum.phalconphp.com/discussion/510/using-acl-in-phalcon-1-1-0