Zend Framework - Controller - HeadScript - 在`composer update`之后不加载

时间:2013-05-23 06:02:17

标签: zend-framework2 zend-view

我是Zend Framework(2)的新手。我一直在开发一个迷你ERP作为一个项目,每件事情似乎都很好。

但是今天早上,我想安装PHPUnit,我更新了composer.json文件并运行了composer install,但它保留了nothing to install。然后经过一些简短的搜索,我注意到我应该运行composer update。它确实将Zend Framework更新为2.2.0和其他一些内容。 Zend曾经是2.0.8

我运行了应用程序,看起来一切都很好,直到我的合作伙伴抱怨演示失败。

我诊断出问题,这是由于没有加载JavaScript文件引起的。视图所需的JavaScripts通过控制器给出如下。

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

认为这有点像文件未找到的问题,我更改了文件路径,就像它在公共文件夹/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>&copy; 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>

2 个答案:

答案 0 :(得分:1)

您正尝试在ControllerLevel上使用ViewHelper。这是不可能的。 Controller中的$this->blubb()等速记函数称为ControllerPlugins。您可以获取所有ControllerPlugins权利here at Zend\Mvc\Controller\Plugin\*

的列表

如果要在Controller级别访问ViewHelper,则需要访问ViewHelperManager。这是通过ServiceManager以下列方式完成的:

$vhm        = $this->getServiceLocator()->get('viewhelpermanager');
$headScript = $vhm->get('headscript');

$headScript->appendFile(/** ... */);

没有测试它,因为它不是必需的,但它肯定应该工作。如果它不起作用,请告诉我;)

答案 1 :(得分:0)

我修改了上面的脚本。

这是经过验证的工作代码。

$vhm = $this->getServiceLocator()->get('ViewHelperManager');
$headScript = $vhm->get('headscript');
$headScript->appendFile( $url );