Is there a way to redirect the browser from the bootstrap in Zend Framework?

北战南征 提交于 2019-12-02 00:12:46

more than year later, i'm programming in ZF and I got this solution for your problem. Here is my function in bootstrap that determines where the user is logged on.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

{

protected $_front;

(...)

protected function _initViewController() 
{
    (...)

    $this->bootstrap('FrontController');
    $this->_front = $this->getResource('FrontController');

    (...)
}

protected function _initLogado()
{
    $router = $this->_front->getRouter();
    $req = new Zend_Controller_Request_Http();
    $router->route($req);
    $module = $req->getModuleName();

    $auth = Zend_Auth::getInstance();
    if ($auth->hasIdentity()) {
        $this->_view->logado = (array) $auth->getIdentity();
    } else {
        $this->_view->logado = NULL;
        if ($module == 'admin') {
            $response = new Zend_Controller_Response_Http();
            $response->setRedirect('/');
            $this->_front->setResponse($response);
        }
    }
}

}

Redirection should really not be in the bootstrap file... That will be one horrible night of debugging for the coder that ends up stuck with your code in a few years.

Use either a Front Controller Plugin, Action Controller Plugin, or do it in your Action Controller. Ultimately such a redirect should be avoided altogether...

The best way is probably a Controller Plugin

You can add a routeShutdown() hook that is called after routing has occured, but before the action method your controller is called. In this plugin you can then check the request data or maybe look for permissions in an ACL, or just redirect at random if that's what you want!

The choice is yours!

EDIT: Rereading your question, it looks like you're not even interested in the route - use routeStartup() as the earliest point after bootstrapping to inject your code.

I would grab the router from the front controller and call its assemble() method and then use header() :)

Regards,

Rob...

You can check the condition on "routeShutdown" method in plugin and then use $this->actionController->_helper->redirector() to redirect ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!