I'm working on an ACL which is called in Module.php and attached to the bootstrap.
Obviously the ACL restricts access to certain areas of the site, which brings the need for redirects. However, when trying to use the controller plugin for redirects it doesn't work as the plugin appears to require a controller.
What's the best way to redirect outside from outside of a controller? The vanilla header() function is not suitable as I need to use defined routes.
Any help would be great!
Cheers-
In general, you want to short-circuit the dispatch process by returning a response. During
route
ordispatch
you can return a response to stop the usual code flow stop and directly finish the result. In case of an ACL check it is very likely you want to return that response early and redirect to the user's login page.You either construct the response in the controller or you check the plugin's return value and redirect when it's a response. Notice the second method is like how the PRG plugin works.
An example of the first method:
An example like the PRG plugin works:
The plugin could then look like this (in the second case):
In your question you say:
This can be a problem inside the controller plugin when you want to do
$this->getController()
in the plugin. You either must extendZend\Mvc\Controller\Plugin\AbstractPlugin
or implementZend\Mvc\Controller\Plugin\PluginInterface
to make sure your ACL plugin is injected with the controller.If you do not want this, there is an alternative you directly return a response you create yourself. It is a bit less flexible and you create a response object while there is already a response object (causing possible conflicts with both responses), but the plugin code would change like this: