如何控制js文件加载布局,从控制器(Zend框架)


How to control the js files that are load in layout, from the Controller (Zend Framework)

首先,我是Zend框架的初学者,也许有一种简单的方法可以做到这一点,但我不知道。

我想要实现的是加载不同的js文件(在布局头)取决于什么控制器被使用。我不能将js文件作为变量添加到布局对象,因为布局在那些控制器中被禁用。

正确的做法是什么?谢谢你。

编辑(代码)

在CalendarController中(使用Subdigger的方法):

    public function init()
    {
      $this->_helper->layout->disableLayout();
      $js = new Application_View_Helper_Javascript();
      //get an array with the basename of the js files          
      $jsFiles = $js->addFiles('calendar');
      foreach ($jsFiles as $k=>$file){
           $this->view->headScript()->appendFile('/js/' . $file.'.js');
      }

    }
在layout. php:
<?php
 echo $this->doctype()."'n";
 ?>
 <html>
    <head>
       <?php
         echo $this->headMeta()."'n";
         echo $this->headLink()."'n";
         echo $this->headTitle()."'n";
         echo $this->headScript()."'n";

你可以这样做:在控制器

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        $this->view->headScript()->appendFile(
          '/js/prototype.js',
          'text/javascript',
          array('conditional' => 'lt IE 7')
        );
    }
......
}

then in view:

.....
  <head>
    <?php echo $this->headScript() ?>
  </head>
.....

读这本

我理解你的问题,但不明白你的意图。你在注释中说布局在indexController中是启用的,但其他的布局是禁用的。如果你分派到另一个控制器并禁用布局,布局。不应该使用php。那么,如何使用它呢?你们在什么地方有包含吗?

我认为这里的解决方案不是禁用布局,而是在那些"其他"控制器中加载不同的布局。

$this->_helper->layout->setLayout('foobaz');

你可以为每个控制器加载不同的布局,甚至是一个控制器中的动作。我注意到的另一件事是,您在init()方法中加载它。我在动作或pre/postDispatch方法中完成所有这些逻辑。