I am writing a module for ACL
in ZF2
, And I am almost done with it.
The point where I am stucked is when user is not authorised to access the requested page, I want to forward the user to a page showing 403
message.
I have tried redirecting user to 403
but it updates URL
, so now I am tring to forward user.
All I want to do is from Module.php
. I have tried below code -
Module.php
if (!$isAllowed) {
$e->getApplication()->getServiceManager()->get('ControllerPluginManager')->get('forward')->dispatch('acl');
}
Using this I got following error -
Uncaught exception 'Zend\Mvc\Exception\DomainException' with message 'Forward plugin requires a controller that implements InjectApplicationEventInterface'
I have also tried to implement Acl
controller with InjectApplicationEventInterface
, But the issue remains same.
Can you please explain how to Forward
to another Action
from Module.php
?
Let me know if you need more details.
When I implemented ACL, I created my own AUTH module for authorization and authentication.
In that module I created ACL plugin.. something like below
//Module.php
//Auth/src/Auth/Controller/AclPlugin
I think that if you want to send information to user that he is in restricted zone without the update of url you have to:
After one of this actions you just change Response to 403. If that`s what you want its simple to do. For any action in your controller you want to send 403 status just use:
or if you want to change layout for your custom one:
You can of course do it to in bootstrap section of your module adding listener on event_dispatch and there check if $acl->isAllowed() after that do your changes which i wrote above. Example:
What you can do is to listen for the dispatch event. You can update the route match during this event to match a controller/action pair which is defined by yourself to render the 403 page.
In code:
Forwarding is a pattern to dispatch a controller when another controller has already been dispatched. That is not your case, as I read it from your question. So don't use the forward plugin, but modify the route match before it's dispached.
You can not forword but redirect to your 403 page as follow: