php - 在 zend 框架中包含 js 文件的最佳方式

标签 php javascript zend-framework

我想在我的 php 脚本中包含外部 js 文件。我正在关注 zend 框架。现在我正在像这样在 Controller 的 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');
}

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

最佳答案

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

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

<!-- 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>

然后您的 View 脚本可以将 Assets 添加到在布局中回显的助手。由于布局使用了prepend*() 方法,全局文件将首先显示,例如

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

关于php - 在 zend 框架中包含 js 文件的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10100197/

相关文章:

zend-framework - Zend 框架 : Insert DIV and Image in my form

php - 跨多个服务器使用 Zend Cache 和 AWS ElastiCache 的缓存值不一致

php - 如何使用 php 和 jquery 验证向导表单?

javascript - 从两个对象数组中过滤,然后合并它们

javascript - 如何在 raphaeljs 中使用 attr 的 stroke-dasharray,stroke-linecap,stroke-linejoin

javascript - 使用 Node JS 将 txt 文件转换为 json

php - Zend 验证 Db_NoRecordExists 和排除选项

php - 基于自定义字段的每个购物车项目的 WooCommerce 自定义数量输入步骤

php - 使用 php 和 html 选择框过滤 SQL 查询

php - cURL cookie 值