【问题标题】:Zend Framework Placeholder doesn't work when used in another actionZend Framework 占位符在其他操作中使用时不起作用
【发布时间】:2014-05-25 04:23:21
【问题描述】:

使用 Zend Framework 1,我目前被占位符困扰,真的不明白问题出在哪里。

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

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

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

这一行:

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

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

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

有什么线索吗?

(我在做一个不使用 Zend_Layout 的项目,不知道会不会有什么后果)

【问题讨论】:

  • 请进一步解释:您是否希望new action 为您希望在main action 输出中呈现的segment 占位符设置内容?
  • 是的,这正是我想要的
  • 问题可能是,视图是您的 main.phtml 视图脚本,它在 mainAction 完成后呈现。所以占位符是在 mainAction 完成时呈现的。在 mainAction 之后更改为另一个 Action,这就是 ActionStack Helper 所做的,只是不发送响应并完成调度过程,而是从堆栈中调用下一个动作。也许您应该阻止在 mainAction 中渲染视图,而是在您的操作堆栈中的最后一个操作中渲染视图。
  • 在我看来,视图是在每个动作运行后呈现的。所以对Zend的路由MVC有误解。谢谢你。

标签: zend-framework zend-view


【解决方案1】:

Zend Action Stack Helper 有一些东西真的很糟糕,这就是为什么我建议你根本不要使用这个帮助器。

这里有一些关于我为什么推荐它的信息:

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

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

也许可以告诉我们您想做什么,我们可以尝试为您提供一些其他想法和示例,说明如何以不同的方式解决您的问题。

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

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

假设您的 Application 控制器文件夹中有一个 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;
    }
}

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

echo $this->segment;

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 2013-10-19
    • 2017-04-23
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多