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

試著忘記壹切 提交于 2019-12-13 07:29:06

问题


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');


来源:https://stackoverflow.com/questions/40269556/how-to-add-js-or-css-files-to-the-end-of-the-queue-from-the-controller-action-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!