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

2021-12-29 00:00:00 php javascript zend-framework

我想在我的 php 脚本中包含外部 js 文件.我正在关注 zend 框架.现在我正在像这样在控制器的 init 函数中添加 js 文件.

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

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

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

推荐答案

JavaScript(和图像、CSS、Flash 电影等)属于视图层,所以在那里配置它们.

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>

然后您的视图脚本可以将资产添加到在布局中回显的助手.由于布局使用了prepend*()方法,所以会先显示全局文件,例如

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

相关文章