我对 Zend Framework (2) 很陌生。我一直在开发一个迷你 ERP 作为一个项目,一切似乎都很好。
但是今天早上,我想安装 PHPUnit,我更新了 composer.json 文件并运行了composer install
,但它说nothing to install
。然后经过一些简短的搜索,我注意到我应该运行composer update
。它确实更新Zend Framework
到2.2.0
和其他一些。Zend 曾经是2.0.8
。
我运行了应用程序,一切看起来都很好,直到我的伙伴抱怨演示失败。
我诊断出这个问题,这是由于没有加载 JavaScript 文件引起的。视图所需的 JavaScript 是通过控制器提供的,如下所示。
public function viewContactAction(){
// Get the user id from url
$id = $this->params()->fromRoute('id');
$this->headScript = new HeadScript();
$this->headScript->appendFile('../../js/pages/lib/contact.view.js');
$this->headScript->appendFile('../../js/packages/json-populate/dist/jquery.jsonPopulate.min.js');
$view = new ViewModel(array('title' => 'Contact View', 'contact_id' => $id));
$view->setTemplate('contacts/contacts/contact');
return $view;
//die("User View for $id");
}
然后我查看了Application
model
. 它似乎使用了一些不同的东西。我将其更新如下。
public function viewContactAction(){
// Get the user id from url
$id = $this->params()->fromRoute('id');
//$this->headScript = new HeadScript();
$this->headScript()->appendFile('../../js/pages/lib/contact.view.js');
$this->headScript()->appendFile('../../js/packages/json-populate/dist/jquery.jsonPopulate.min.js');
$view = new ViewModel(array('title' => 'Contact View', 'contact_id' => $id));
$view->setTemplate('contacts/contacts/contact');
return $view;
//die("User View for $id");
}
认为这可能是文件未找到问题,我更改了文件路径,就好像它在 public 文件夹中一样/js/pages/lib/contact.view.js
,但文件仍然没有显示。
是否不再支持在控制器中使用 HeadScript?还是方式变了?提前致谢。
好的,这是我的布局文件。除了添加一些js之外,我不记得对其进行任何更改。
<?php echo $this->doctype(); ?>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo $this->headTitle('ZF2 '. $this->translate('Skeleton Application'))->setSeparator(' - ')->setAutoEscape(false) ?>
<?php echo $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0') ?>
<!-- Le styles -->
<?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/images/favicon.ico'))
->prependStylesheet($this->basePath() . '/css/bootstrap-responsive.min.css')
->prependStylesheet($this->basePath() . '/css/style.css')
->prependStylesheet($this->basePath() . '/css/bootstrap.min.css') ?>
<!-- Scripts -->
<?php echo $this->headScript()->prependFile($this->basePath() . '/js/html5.js', 'text/javascript', array('conditional' => 'lt IE 9',))
->prependFile($this->basePath() . '/js/bootstrap.min.js')
->prependFile($this->basePath() . '/js/jquery.min.js')
->appendFile($this->basePath() . '/js/jquery.konnections.tableDefinition.js')
->appendFile($this->basePath() . '/js/jquery.konnections.appendTemplateFromJSON.js'); ?>
<?php //echo $this->headScript; ?>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Skeleton Application') ?></a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Home') ?></a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<?php echo $this->translate('Contacts'); ?>
</a>
<ul class="dropdown-menu">
<li><a href='contacts'>Contact Table</a></li>
<li><a href='contacts/add-contact'>Add New Contact</a></li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<?php echo $this->content; ?>
<hr>
<footer>
<p>© 2005 - 2012 by Zend Technologies Ltd. <?php echo $this->translate('All rights reserved.') ?></p>
</footer>
</div> <!-- /container -->
<?php echo $this->inlineScript() ?>
</body>
</html>
生成的源看起来像这样(页面有点长,所以只包含标题)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ZF2 Skeleton Application</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Le styles -->
<link href="/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/style.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/bootstrap-responsive.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">
<!-- Scripts -->
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<!--[if lt IE 9]><script type="text/javascript" src="/js/html5.js"></script><![endif]-->
<script type="text/javascript" src="/js/jquery.konnections.tableDefinition.js"></script>
<script type="text/javascript" src="/js/jquery.konnections.appendTemplateFromJSON.js"></script> </head>