views:

903

answers:

2

Hi, I'm trying to centralise my redirects (based on authentication and various other states) into a front controller plugin. So far I've tried:

    $this->setRequest(new Zend_Controller_Request_Http('my_url'));

at various points in the plugin (i.e. from routeStartup to dispatchLoopShutdown) and also:

    $this->setResponse(new Zend_Controller_Response_Http('my_url'));

Can anyone offer some assistance on this, or point me in the direction of a tutorial?

+1  A: 

If you are looking to redirect if the user is not logged it, the first parameter of dispatchLoopStartup() is a handle to the request object.

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
    if(!Zend_Auth::getInstance()->hasIdentity())
    {
        $request->setControllerName('auth');
        $request->setActionName('login');
        // Set the module if you need to as well.
    }
}
smack0007
ah - I think that's what I was looking for. I want to avoid using the redirector (which is already in use) and intercept the request at source. Thanks for your help, I'll give it a whirl.
sunwukung
Note that this does not issue a HTTP redirect or create a new request, but forwarding/modifying the request IS better - nice answer.
David Caunt
Redirecting and forwarding are often confused in Zend Framework. It's important to understand the differences. In his question he says redirect but I think what he actually means is forwarding.
smack0007
I've tried this approach, my dilemma now is that while the request has been modified, it is not redirecting/forwarding the user until the page has been refreshed once. I'm guessing I have to issue a fresh HTTP request then?
sunwukung
Can you edit your question and post your new code?
smack0007
Ah, I got it working - sorry. I had to check the post array to see if the user was trying to logout. Thanks for your help
sunwukung
+2  A: 

The easiest way would be to use ZF's Redirect ActionHelper

    $r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
    $r->gotoUrl('/some/url')->redirectAndExist();

Alternatively instantiate it without the HelperBroker

    $r = new Zend_Controller_Action_Helper_Redirector;
    $r->gotoUrl('/some/url')->redirectAndExist();

The ActionHelper provides an API solely concerned about redirecting through a number of methods, like gotoRoute, gotoUrl, gotoSimple, which you can use depending on your desired UseCase.

Internally, the ActionHelper uses the APIs of Response and Router to do the redirect though, so you can also use their methods directly, e.g.

    $request->setModuleName('someModule')
            ->setControllerName('someController')
            ->setActionName('someAction');

or

    $response->setRedirect('/some/url', 200);

Further reading:

Gordon
thanks for that, I should have mentioned that I'm trying to avoid using the redirector (which I'm using in the controllers already), and want to intercept the request/response directly. Thanks for the links though, I'll give them a look tomorrow.
sunwukung