Best way to include js file in zend-framework

I want to include an external js file in my php script. I follow the zend framework. Now I am adding a js file to an init controller function like this.

public function init() { $this->doUserAuthorisation(); parent::init(); $this->view->headScript()->appendFile($this->view->baseUrl().'/js/front_cal/jquery-1.3.2.min.js'); $this->view->headLink()->setStylesheet($this->view->baseUrl().'/styles/front_cal/calendar.css'); } 

the problem i am facing is the js file doesnot include.Is this the correct way to include the js file?

+6
source share
2 answers

JavaScript (and images, CSS, flash movies, etc.) belong to the presentation layer, so customize them there.

For files with global inclusion, add them to the layout, for example

 <!-- layout.phtml --> <head> <?php echo $this->headScript()->prependFile( $this->baseUrl('path/to/file.js')) ?> <?php echo $this->headLink()->prependStylesheet( $this->baseUrl('path/to/file.css')) ?> <!-- snip --> <?php echo $this->inlineScript()->prependFile( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js') ?> </body> 

Your view scripts can then add assets to assistants, which are reflected in the layout. Since the layout uses prepend*() methods, global files will be displayed first, for example

 <?php // views/scripts/index/index.phtml $this->inlineScript()->appendFile($this->baseUrl('path/to/script.js')); 
+12
source

In the above solution, the path /to/file.js' script will be included in the header twice. There is no need for echo before calling prependFile . This behavior can lead to duplication of javascript controls.

<!-- layout.phtml --> <head> <?php echo $this->headScript()->prependFile( $this->baseUrl('path/to/file.js')) // this will echo out the whole prependFile queue

+2
source

Source: https://habr.com/ru/post/912899/


All Articles