如何在Zend框架中从控制器动作中添加JS或CSS文件到队列的末尾


How to add JS or CSS files to the end of the queue from the controller action in Zend Framework?

我已经阅读了很多关于这方面的主题(列表在帖子的末尾),但我不能使我的代码工作。首先,我使用旧的Zend框架1.11。我有以下布局(只是一个例子):

<?php
    $this->headScript('file','/js/jquery/jquery-1.12.4.min.js');
    $this->headScript('file','/js/jquery/jquery-migrate-1.4.1.min.js');
    // more CSS & JS files goes here
    // some PHP code  goes here
?>
<html>
    <head>
        <?php
           echo $this->headLink();
           echo $this->headStyle();
           echo $this->headScript();
           echo $this->headInline();
        ?> 
    </head>
    ...
</html> 

这是控制器上的函数:

public function indexAction()
{
    $this->view->headScript()->appendFile('/js/jsgrid.js', 'text/javascript');
    $this->_helper->layout->setLayout('admin_console');
}

jsgrid.js的代码依赖于要加载的jQuery。我想把这个文件作为最后一个文件从控制器加载所以结果应该是这样的:

<script type="text/javascript" src="/js/jquery/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="/js/jquery/jquery-migrate-1.4.1.min.js"></script>
<script type="text/javascript" src="/js/jsgrid.js"></script>

但是从上面的代码中,我得到了这个:

<script type="text/javascript" src="/js/jsgrid.js"></script>
<script type="text/javascript" src="/js/jquery/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="/js/jquery/jquery-migrate-1.4.1.min.js"></script>

导致脚本失败。我怎样才能做到这一点?任何建议吗?

主题列表:

  • ZendFramework -如何从控制器添加->HeadScript() ?
  • Zend 1.12:将javascript附加到视图的底部
  • 在zend框架中包含js文件的最佳方式
  • https://framework.zend.com/manual/2.4/en/modules/zend.view.helpers.head-script.html
  • http://www.stoimen.com/blog/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/

经过几次尝试,我终于找到了解决方案:

$this->view->inlineScript()->appendFile('/js/jsgrid.js', 'text/javascript');