在zend框架中包含js文件的最佳方法

Ruk*_*tel 6 javascript php zend-framework

我想在我的PHP脚本中包含外部js文件.我正在关注zend framework.Right现在我在控制器的init函数中添加js文件,就像这样.

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');
}
Run Code Online (Sandbox Code Playgroud)

我面临的问题是,js文件不包含.这是包含js文件的正确方法吗?

Phi*_*hil 12

JavaScript(以及图像,CSS,flash电影等)属于视图层,因此在那里配置它们.

对于全局包含的文件,将它们添加到您的布局中,例如

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

然后,您的视图脚本可以将资源添加到布局中回显的帮助程序.当布局使用prepend*()方法时,将首先显示全局文件,例如

<?php // views/scripts/index/index.phtml
$this->inlineScript()->appendFile($this->baseUrl('path/to/script.js'));
Run Code Online (Sandbox Code Playgroud)