How to add JS or CSS files to the end of the queue

2019-09-06 01:37发布

问题:

I have read a lot of topics about this (list at the end of the post) but I can't make my code to work. First I am using old Zend Framework 1.11. I have the following layout (just an example):

<?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> 

And this is the function at my controller:

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

The code at jsgrid.js depends on jQuery to be loaded. I want to load the file from the controller as the last one so the result would be something like:

<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>

But from the code above I am getting this instead:

<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>

Which is causing the script to fail. How I can achieve this? Any advice?

List of topics:

  • ZendFramework - How to add ->HeadScript() from Controllers?
  • Zend 1.12: Append javascript to bottom of view
  • the best way to include js file in zend framework
  • 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/
  • and .... a lot more

回答1:

After several tries finally I have found the solution:

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