Redirect in Front Controller plugin Zend

28,968

Solution 1

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.
    }
}

Solution 2

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

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

Alternatively instantiate it without the HelperBroker

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

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:

Solution 3

If you want to redirect in the index page then this should suffice.

public function preDispatch(Zend_Controller_Request_Abstract $request)
{
    if(!Zend_Auth::getInstance()->hasIdentity())
    {
          $baseUrl = new Zend_View_Helper_BaseUrl();
          $this->getResponse()->setRedirect($baseUrl->baseUrl());
    }
}

If you want to redirect somewhere else then just change the parameter in the setRedirect() function

Thanks! :)

Share:
28,968
sunwukung
Author by

sunwukung

Professional LAMP stack web developer proficient in following languages: PHP, Zend, (My)SQL (also HTML, CSS). Frameworks: Zend, jQuery, YUI, _.js Interested in: Scheme, Python, Lua Hobbies: Martial Arts, reading, XBox - the usual suspects Fave Bands: Metronomy, Starkey, Walls, Tycho.

Updated on July 09, 2022

Comments

  • sunwukung
    sunwukung almost 2 years

    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?

  • sunwukung
    sunwukung over 14 years
    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
    sunwukung over 14 years
    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.
  • David Snabel-Caunt
    David Snabel-Caunt over 14 years
    Note that this does not issue a HTTP redirect or create a new request, but forwarding/modifying the request IS better - nice answer.
  • smack0007
    smack0007 over 14 years
    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.
  • sunwukung
    sunwukung over 14 years
    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
    sunwukung over 14 years
    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
  • Rafael Kassner
    Rafael Kassner about 12 years
    According to stateless concept of HTTP, this is wrong. If you "redirect" by this way, the URL will be the same and then, and you are serving totally different contents in the same URL. Anyway, if this is "the Zend Framework way" to accomplish it, thanks.