最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - the best way to include js file in zend framework - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Apr 11, 2012 at 5:17 Rukmi PatelRukmi Patel 2,5619 gold badges29 silver badges42 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

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

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

发布评论

评论列表(0)

  1. 暂无评论