简体   繁体   English

将变量从Controller / Action传递给Layout时,如何避免重复

[英]How do I avoid repetition when passing variables from the Controller/Action to the Layout

I am currently working on a project developed using Zend Framework, based on the structure of my web page design I have reached a point where I have to pass a small number of variables to my layout from each Controller/Action. 我目前正在开发一个使用Zend Framework开发的项目,基于我的网页设计结构,我已经达到了一个点,我必须将每个Controller / Action中的少量变量传递给我的布局。 These variables are: 这些变量是:

<?php Zend_Layout::getMvcInstance()->assign('pageId', 'page1'); ?>
<?php Zend_Layout::getMvcInstance()->assign('headerType', '<header id="index">'); ?>

The reason for passing this information is firstly, I pass the page id as the multi column layout may change depending on the content being displayed, thus the page id within the body tag links the appropriate CSS to how the page should be displayed. 传递此信息的原因首先是,我传递了页面ID,因为多列布局可能会根据显示的内容而改变,因此body标签内的页面ID将相应的CSS链接到页面应该如何显示。 Secondly I display a promotional jQuery slider only on the index page, but I need the flexibility to have it displayed on potentially multiple pages in case the wind changes and the client changes their mind. 其次,我只在索引页面上显示促销jQuery滑块,但我需要灵活地将它显示在可能的多个页面上,以防风改变并且客户改变主意。

My actual question: Is there a more appropriate method of passing this information to the Layout that I am overlooking? 我的实际问题: 是否有更合适的方法将此信息传递给我忽略的布局?

I am not really questioning whether the information has to be sent, rather is there some Zend Framework feature that I have, in my haste, overlooked which would reduce the amount of repetitive redundant code which may very well be repeated in multiple Actions within the same controller? 我并没有真正质疑是否必须发送信息,而是我有一些Zend Framework功能,我的匆忙,忽略了这将减少重复冗余代码的数量,这可能很好地在多个动作中重复控制器?

You shoudn't really be passing anything from the view to the layout, for a start the view should be included IN the layout, not the other way around. 你不应该从视图到布局传递任何东西,因为一开始视图应该包含在布局中,而不是相反。

So, setting your page title should be done using similar code to what you have, but inside the controller action being called: 因此,设置页面标题应使用与您拥有的代码类似的代码来完成,但在调用的控制器操作中:

$this->view->headTitle()->append('Homepage');

And the other two issues - you need to rethink as I stated to begin with. 还有另外两个问题 - 你需要重新思考,正如我所说的那样。 Maybe you're misunderstanding the layout/view principle? 也许你误解了布局/视图原理? If you include the different views per action, then you simply change the div id when needed, and include the header for your banner only in the index.phtml file. 如果您为每个操作包含不同的视图,那么您只需在需要时更改div id,并仅在index.phtml文件中包含横幅的标题。

You could turn that logic into an action helper than you can call from your controllers in a more direct way. 您可以将该逻辑转换为动作帮助程序,而不是以更直接的方式从控制器中调用。 You could also make a view helper to accomplish the same thing but view helpers usually generate data for the view rather than set properties. 您还可以创建一个视图帮助程序来完成相同的操作,但查看帮助程序通常会为视图生成数据而不是设置属性。

// library/PageId.php
class Lib_PageId extends Zend_Controller_Action_Helper_Abstract
{
    public function direct($title, $pageId, $headerType)
    {
        $view = $this->getActionController()->view;


        $view->headTitle()->append($title);
        $view->pageId = $pageId;
        $view->headerType = $headerType;
    }
}

In your controller actions you can now do this: 在控制器操作中,您现在可以执行以下操作:

$this->_helper->PageId('Homepage', 'page1', 'index');
// now pageId and headerType are available in the view and
// Homepage has been appended to the title

You will also need to register the helper path in your Bootstrap like this: 您还需要在Bootstrap中注册辅助路径,如下所示:

protected function _initActionHelpers()
{
    Zend_Controller_Action_HelperBroker::addPrefix('Lib');
}

Doing it like that can reduce the amount of repetitive code and remove needing to assign the values from the view. 这样做可以减少重复代码的数量,并删除需要从视图中分配值。 You can do it in the controller very quickly. 您可以非常快速地在控制器中执行此操作。 You can also have default values in the case that the helper hasn't been called. 在未调用帮助程序的情况下,您也可以使用默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM