Reputation: 112
i've some difficult to exactly understand the redirect behavior.
Pratically i've a Controller that call some Interceptor classes
...
$i->intercept();
...
these classes do some work and may decide to redirect request to another Controller\Action.
...
$redirector->toRoute($route, array(
'action' => $action,
));
But this line seems to do nothing, in fact in the page is show the content of the originally called action. Calling 1 the original action and 2 the action after redirect, what i want is to execute the action 2 and render the related view and not execute the code of action 1.
What i'm doing wrong?
Thanks a lot
Upvotes: 0
Views: 204
Reputation: 13558
The usual flow in the application is bootstrap > routing > dispatching (a controller runs) > rendering. If you want to perform a redirect, that happens in your case at the controller level (dispatch). So, you want to stop the normal flow and don't want to enter the render phase.
In Zend Framework you can stop this loop by returning a Response object. If you return a response anywhere during routing or dispatching, the flow stops and that response object is returned to the browser. You have to understand the redirect plugin creates in fact a Response object:
public function fooAction()
{
// Do some work in this controller action
return $this->redirector()->toRoute('bar');
}
The redirector method toRoute()
returns a Response and in the controller this response object is returned. So, the application stops the usual flow and sends the response to the browser.
You have to catch the result of your interceptor class, check if it's a response and if so, return it. Of course you have to return the result of the redirect in your class.
So step #1, return the response in your interceptor class:
return $redirector->toRoute(/* ... */);
And step #2, check the return value of your interceptor class:
use Zend\Http\Response;
$result = $i->intercept();
if ($result instanceof Response) {
return $result;
}
Upvotes: 2