ZF2 Use redirection outside the controller

I am working on an ACL that is called in Module.php and attaches to the bootstrap.

Obviously, the ACL restricts access to certain areas of the site, which leads to the need for redirection. However, when trying to use the controller plugin for redirection, it does not work, because the plugin appears to require a controller.

What is the best way to redirect outside from outside the controller? The vanilla () header function is not suitable as I need to use specific routes.

Any help would be great!

Cheers -

+4
source share
1 answer

In general, you want a short-circuited sending process, returning a response. During route or dispatch you can return a response to stop the usual stop of the code stream and directly complete the result. In the case of ACL verification, it is very likely that you want to return the response earlier and redirect to the user login page.

You either create a response in the controller, or check the return value of the plugin and redirect its response. Note that the second method is similar to how the PRG plugin works .

An example of the first method:

 use Zend\Mvc\Controller\AbstractActionController; class MyController extends AbstractActionController { public function fooAction() { if (!$this->aclAllowsAccess()) { // Use redirect plugin to redirect return $this->redirect('user/login'); } // normal code flow } } 

An example of using the PRG plugin:

 use Zend\Mvc\Controller\AbstractActionController; use Zend\Http\Response; class MyController extends AbstractActionController { public function fooAction() { $result = $this->aclCheck(); if ($result instanceof Response) { // Use return value to short-circuit return $result } // normal code flow } } 

Then the plugin could look like this (in the second case):

 use Zend\Mvc\Controller\Plugin\AbstractPlugin; class AclCheck extends AbstractPlugin { public function __invoke() { // Check the ACL if (false === $result) { $controller = $this->getController(); $redirector = $controller->getPluginManager()->get('Redirect'); $response = $redirector->toRoute('user/login'); return $response; } } } 

In your question you say:

[...] it does not work, because a plug-in requires a controller.

This can be a problem inside the controller plugin if you want to do $this->getController() in the plugin. You must either extend Zend\Mvc\Controller\Plugin\AbstractPlugin or implement Zend\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 in which you directly return an answer that you create yourself. This is a little less flexible, and you create a response object, while there is already a response object (causing possible conflicts with both answers), but the plugin code will change as follows:

 use Zend\Mvc\Controller\Plugin\AbstractPlugin; use Zend\Http\PhpEnvironment\Response; class AclCheck extends AbstractPlugin { public function __invoke() { // Check the ACL if (false === $result) { $response = new Response; $response->setStatusCode(302); $response->getHeaders() ->addHeaderLine('Location', '/user/login'); return $response; } } } 
+9
source

All Articles