I have a new ZF3 application with ACL. Now I need to, in case of unauthorized access, redirect to the page with the error (for example, 403). I think the best way is to fire an event and then catch it, but I failed ...
Everything is in my user module, in (excerpts): Module.php
namespace User;
use Zend\Mvc\MvcEvent;
use Zend\Permissions\Acl\Acl;
use Zend\Stdlib\Response ;
use Zend\View\Model\ViewModel;
[...]
class Module implements ConfigProviderInterface
{
[...]
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkProtectedRoutes'), 1);
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'dispatchError'), -999);
$this->initAcl($e);
}
public function initAcl(MvcEvent $e)
{
[...]
}
public function checkProtectedRoutes(MvcEvent $e)
{
[...]
$e->setError('ACL_ACCESS_DENIED') ;
$e->setParam('route', $route);
$e->getTarget()->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $e);
}
public function dispatchError(MvcEvent $e)
{
$error = $e->getError();
switch ($error) {
case 'ACL_ACCESS_DENIED' :
break;
default:
return ;
break;
}
return false ;
}
}
But when my event fires, my method is dispatchError()never called, and the cry of ZF3:
: 1 Zend\Mvc\View\Http\RouteNotFoundStrategy:: detectNotFoundError() Zend\Mvc\MvcEvent, Zend\EventManager\Event, /xxxxxxx/vendor/zendframework/zend -eventmanager/src/EventManager.php 271 /xxxxxxxx/vendor/zendframework/zend -mvc/src/View/Http/RouteNotFoundStrategy.php 135
/ ?