NotFoundPlugin not working

Hi,

My app is based on INVO Security and NotFound plugins. I managed to get the ACL in Security to work, but not the NotFound.

Whenever i type http://localhost/somerubbishtext, the NotFound Plugin Exception is not triggered. Instead, the page got redirected to /login/index

Services.php

/**
 * The Events Manager
 */
$di->set('dispatcher', function() use ($di) {

$eventsManager = new EventsManager;

/**
 * Handle access control list using SecurityPlugin
*/
$eventsManager->attach('dispatch:beforeDispatch', new Security);   

/**
 * Handle exceptions and not-found exceptions using NotFoundPlugin
 */
$eventsManager->attach('dispatch:beforeException', new NotFound);

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

return $dispatcher;
});

NotFound.php

public function beforeException(Event $event, MvcDispatcher $dispatcher, Exception $exception)
{
    if ($exception instanceof DispatcherException) {
        switch ($exception->getCode()) {
            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:

                $dispatcher->forward(array(
                    'controller' => 'errors',
                    'action' => 'index'
                ));

                return false;
        }
    }

    $dispatcher->forward(array(
        'controller' => 'errors',
        'action'     => 'index'
    ));

    return false;
}

Security.php

public function beforeDispatch(Event $event, Dispatcher $dispatcher)
{
    $auth = $this->session->get('auth');

    if (!$auth){
        $role = 'Guests';
    } else {
        $role = 'Users';
    }

    $controller = $dispatcher->getControllerName();
    $action = $dispatcher->getActionName();

    $acl = $this->getAcl();
    $allowed = $acl->isAllowed($role, $controller, $action);

    //var_dump($acl);
    //die;

    if ($allowed != Acl::ALLOW) {

        $dispatcher->forward(array(
            'controller' => 'login',
            'action'     => 'index'
        ));
        return false;
    }

}

Expert Advice appreciated!



2.8k

Maybe this happens because you redirect before the dispatcher recognize, that your route is not known. Because your auth plugin fires before the dispatcher gets started with the route from the browser.



13.0k

I changed it to the following in services.php and it works. Can any one explains why using beforeDispatch doesnt work for me? I am sure INVO shd be working fine.

/**
 * Handle access control list using SecurityPlugin
*/
$eventsManager->attach('dispatch:beforeExecuteRoute', new Security);