Zend Framework占位符在用于其他操作时不起作用

时间:2014-05-22 11:28:29

标签: zend-framework zend-view

使用Zend Framework 1,我目前只使用占位符,实际上不明白问题出在哪里。

我想在主视图中使用占位符呈现一些内容:

<div>
<?= $this->placeholder( 'segment' ); ?>
</div>

我有一个主要操作,我在其中创建页面,但我想使用ActionStack Helper在另一个操作中为此占位符设置渲染。这似乎是不可能的。

这一行:

$this->view->placeholder('segment')->set( 'XXXXXX' );

在主操作上写入时给出预期结果,但在我用ActionStack调用的操作中没有效果。

我尝试在新操作的视图中处理占位符,并在操作代码中处理。没有结果,我的占位符没有给我输出。

任何线索?

(我在一个不使用Zend_Layout的项目上工作,我不知道它是否会产生后果)

1 个答案:

答案 0 :(得分:0)

Zend Action Stack Helper 有些东西非常糟糕,这就是为什么我建议你根本不使用这个帮手。

以下是我建议的原因:

http://www.rmauger.co.uk/2009/03/why-the-zend-framework-actionstack-is-evil/

您应该尝试另一种方法,例如使用您在调度过程中编写的受保护属性的父类。

也许告诉我们你想做什么,我们可以尝试给你一些关于如何以不同方式解决问题的其他想法和例子。

我现在将添加另一种解决方案

我认为您想要做的是在视图脚本中使用的变量中构建某种信息。

假设您应用程序控制器文件夹中有 IndexController.php

class IndexController extends Zend_Controller_Action {
     //add a protected attrib to your class that can be used to transfer and store data
     protected $_segment = '';
     //you can also add setter and getter for this attribute         

     public function mainAction() {
          //you do some things here
          //$this->view->placeholder('segment')->set('xxxxx','test');
          //instead do this
          $this->_segment= 'This is a';
          //build your action stack and add anotherAction to your stack
     }
     public function anotherAction() {
          //$this->view->placeholder('segment')->set('xxxxx','test');
          //instead do the following

          $this->_segment.= ' test';
     }

    /**
     * Is executed before a controller Action is being executed.
     * 
     * @return
     */
    public function preDispatch() {

    }

    /**
     * Is executed after a controller Action is being executed.
     * 
     * @return
     */
    public function postDispatch() {
        //set a view variable at the end of dipatching process
        $this->view->segment = $this->_segment;
    }
}

main.phtml 视图脚本或在此调度过程中呈现的任何其他视图脚本中,您现在可以像使用任何其他viwe变量一样使用构造的视图变量。

echo $this->segment;
相关问题