我想在我的 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/