【问题标题】:Zend redirect within view helperZend 在视图助手中重定向
【发布时间】:2013-04-04 18:16:32
【问题描述】:

您好,我有以下脚本可以在视图助手中重定向

<?php
class Application_View_Helper_ExistUserRev extends Zend_View_Helper_Abstract{

    public function existUserRev($params,$user)
    {
            $businessReviewMapper = new Application_Model_Mapper_BusinessReviewsMapper();
            $businessReviewModel = new Application_Model_BusinessReviews();
        $result = $businessReviewMapper->userReviewStatus($user>getUserId(),$params['bzid']);
            if($result){
            $url = 'http://www.akrabat.com';
            $this->_helper->redirector->gotoUrl($url);
                    }
        }
}
?>

但似乎我上面的重定向似乎不起作用。如何在我的 zend 应用程序的视图助手中重定向?谢谢

【问题讨论】:

  • 您使用的 Zend Fw 版本是什么?
  • @JBRTRND 版本如果没记错的话 1.6 及以上版本

标签: php zend-framework http-redirect


【解决方案1】:

由于您在 View Helper 类中,所以不能使用 $this-&gt;_helper-&gt;redirector-&gt;gotoUrl($url);,这是一个 Action Controller 功能。

您必须在 View Helper 中调用重定向器。
试试这个:

$_redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$_redirector->gotoUrl($url); 

【讨论】:

    【解决方案2】:

    Redirector 是控制器 ACTION 助手,而不是视图助手,因此您应该从控制器而不是视图中使用它。

    要从视图重定向(顺便说一句,这不是一个好主意,逻辑应该保留在控制器中,而不是视图中),请尝试使用 Zend Action View Helper

    【讨论】:

      【解决方案3】:

      这比目前介绍的更简单:

      Zend Framework 1.x 参考摘录: Writing Custom Helpers

      一般来说,类不应回显或打印或以其他方式生成 输出。相反,它应该返回要打印或回显的值。这 返回值应适当转义。

      基本上,视图助手应该返回一个值,而不是执行操作。

      另一方面,Action helpers 几乎可以做任何你需要做的事情。

      这里有一个非常简单的例子来演示在helper中使用direct()方法的形式:

      <?php
      /**
       * Simply returns a search form to a placeholder view helper
       *
       */
      class My_Controller_Action_Helper_Search extends Zend_Controller_Action_Helper_Abstract
      {
          /**
           * @param string $action
           * @param string $label
           * @param string $placeHolder
           * @return \Application_Form_Search
           */
          public function direct($action, $label = null, $placeHolder = null)
          {
              $form = new Application_Form_Search();
              $form->setAction($action);
              $form->search->setLabel($label);
              $form->query->setAttribs(array(
                  'placeholder' => $placeHolder,
                  'size'        => 20,
              ));
              return $form;
          }
      }
      

      这是在控制器中使用它在视图脚本或布局中填充占位符助手的方式。

       public function preDispatch()
          {
              $this->_helper->layout()->search = $this->_helper->search(
                  '/index/display', 'Search My Collection!', 'Search Query'
              );
          }
      

      在视图脚本或布局中:

      <?php echo $this->layout()->search?>
      

      在您的情况下,您可以使用操作助手来建立构建正确 url 所需的值,然后您可以将这些值传递给 url() 助手或您自己构造的助手。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        相关资源
        最近更新 更多