【问题标题】:Zend Framework url redirectZend 框架 url 重定向
【发布时间】:2012-03-30 00:34:00
【问题描述】:
<?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');
        }
    }   
}
}

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

最好的问候,

【问题讨论】:

  • 错误信息是什么?没有什么?根本不重定向?你也可以试试 $this->_helper->redirector() ,或者 $this->_helper->gotoUrl() 在framework.zend.com/manual/en/… 上查看更多信息。
  • 我不能在我的插件中使用这些东西,因为你只能使用$this-&gt;_redirect ..只有在你扩展 Zend_Controller_Action 时才能使用

标签: php zend-framework


【解决方案1】:

...其余代码

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

...其余代码

【讨论】:

    【解决方案2】:
    <?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!
        }
    }
    

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

    【讨论】:

    • 在您的代码中,您可以使用$this-&gt;_redirect,但如果您查看我的代码,您会发现我正在编写一个资产插件控制器。
    • 无论如何,您仍然需要获得帮助。你也可以使用$this-&gt;_redirector-&gt;gotoUrl('/my-controller/my-action/param1/test/param2/test2');
    • 我不明白,为什么我必须创建一个控制器才能使用助手,我想在我的插件中使用那个助手,而不是在我的控制器中。
    • 控制器是我给出的示例,其他用途的方法相同。此时只需阅读框架手册即可。
    【解决方案3】:

    要么使用Zend_Controller_Action_HelperBroker 获取重定向助手,要么直接从请求对象进行重定向。

    请参阅中给出的示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      相关资源
      最近更新 更多