Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the best way to include js file in zend framework

I want to include external js file in my php script. I am following zend framework.Right now I am adding js file in controller's init 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');
}

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

like image 420
Rukmi Patel Avatar asked Apr 11 '12 05:04

Rukmi Patel


People also ask

What is more appropriate way to include JavaScript as an external file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Where should I put my js files?

JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body. JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked.

Is Zend framework in PHP?

Zend is an open source PHP framework. It is pure object-oriented and built around the MVC design pattern. Zend framework contains collection of PHP packages which can be used to develop web applications and services. Zend was started by Andi Gutmans and Zeev Suraski.

What is the use of Zend framework in PHP?

Zend Framework is a collection of professional PHP packages with more than 570 million installations. It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.


2 Answers

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

For globally included files, add them to your layout, eg

<!-- 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 the helpers which are echoed out in the layout. As the layout uses the prepend*() methods, the global files will be displayed first, eg

<?php // views/scripts/index/index.phtml
$this->inlineScript()->appendFile($this->baseUrl('path/to/script.js'));
like image 177
Phil Avatar answered Sep 29 '22 21:09

Phil


In above solution the 'path/to/file.js' script will be included in the header twice. There is no need for echo before calling prependFile. Such behaviour can lead to duplicating javascript controls.

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

like image 34
Karol Avatar answered Sep 29 '22 19:09

Karol