Zend Framework url redirect

懵懂的女人 提交于 2019-12-12 18:19:02

问题


<?php
 class PI_Controller_Plugin_AssetGrabber extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
    /*
        The module name
    */
    $moduleName = $request->getModuleName();
    /*
        This modules requires the user to be loggedin in order to see the web pages!
    */
    $loginRequiredModules = array('admin');

    if (in_array($moduleName,$loginRequiredModules)) {
        $adminLogin = new Zend_Session_Namespace('adminLogin');
        if (!isset($adminLogin->loggedin)) {
            /*--------------------------------------
               Here I want to redirect the user
            */
             $this->_redirect('/something');
        }
    }   
}
}

I'm trying to do a redirect $this->_redirect('/something') but doesn't work! Do you know how can I do a redirect in this case?

Best Regards,


回答1:


Either use Zend_Controller_Action_HelperBroker to get the redirect helper or do the redirect directly from the Request object.

See the examples given in

  • Redirect in Front Controller plugin Zend



回答2:


... rest of code

if (!isset($adminLogin->loggedin)) {
    $baseUrl = new Zend_View_Helper_BaseUrl();
    $this->getResponse()->setRedirect($baseUrl->baseUrl().'/something');
}

... rest of code




回答3:


<?php
class AlternativeController extends Zend_Controller_Action
{
    /**
     * Redirector - defined for code completion
     *
     * @var Zend_Controller_Action_Helper_Redirector
     */
    protected $_redirector = null;

    public function init()
    {
        $this->_redirector = $this->_helper->getHelper('Redirector');
    }

    public function myAction()
    {
        /* Some Awesome Code */

        $this->redirector('targetAction', 'targetController');
        return; //Never reached!
    }
}

You need to get the redirector helper, then you can define the targetAction and targetController with the redirector. That should do it.



来源:https://stackoverflow.com/questions/2433118/zend-framework-url-redirect

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