php Zend 框架 url 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2433118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:27:38  来源:igfitidea点击:

Zend Framework url redirect

phpzend-framework

提问by Uffo

<?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?

我正在尝试进行重定向,$this->_redirect('/something')但不起作用!你知道在这种情况下我该如何进行重定向吗?

Best Regards,

此致,

回答by renzki

... rest of code

... 其余代码

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

... rest of code

... 其余代码

回答by Urda

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

你需要得到redirector helper,然后你可以用redirector定义targetAction和targetController。那应该这样做。

回答by Gordon

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

要么用于Zend_Controller_Action_HelperBroker获取重定向帮助程序,要么直接从 Request 对象进行重定向。

See the examples given in

请参阅中给出的示例