【问题标题】:Zend Framework 2 : Layout only rendering on index actionZend Framework 2:仅在索引操作上呈现布局
【发布时间】:2013-09-09 17:52:34
【问题描述】:

我是 Zend Framework 2 编码的新手,我的布局和控制器有问题:

当我浏览 vhost/ 时,它会呈现我的布局和 index.phtml 视图;所以一切正常!

但是,当我浏览 vhost/cv 时,它只呈现 cv.phtml 而没有我的布局...

这是我的代码

module.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'Portfolio\Controller\Home' => 'Portfolio\Controller\HomeController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'home' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller'    => 'Portfolio\Controller\Home',
                        'action'        => 'index',
                    ),
                ),
            ),
            'cv' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/cv',
                    'defaults' => array(
                        'controller'    => 'Portfolio\Controller\Home',
                        'action'        => 'cv',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

HomeController.php

class HomeController extends AbstractActionController
{

    public function indexAction()
    {
        return new ViewModel();
    }

    public function cvAction()
    {
        return new ViewModel();
    }

}

我尝试使用 2 个控制器,使用子路由,但这些都不起作用...

谁能告诉我我的代码有什么问题?

谢谢, 麦克西姆

编辑:

layout.phtml

<?php echo $this->doctype(); ?>

<html lang='fr'>
    <head>
        <meta charset='utf-8' />

        <link rel='stylesheet' type='text/css' href='<?php echo $this->basePath() . '/css/bootstrap.min.css' ?>' />
        <link rel='stylesheet/less' type='text/css' href='<?php echo $this->basePath() . '/css/base.less'; ?>' />
        <link rel='shortcut icon' href='<?php echo $this->basePath() . '/img/favicon.ico'; ?>' />

         <?php echo $this->headScript()
            ->prependFile($this->basePath() . '/js/ga.js')
            ->prependFile($this->basePath() . '/js/global.js')
            ->prependFile($this->basePath() . '/js/less-1.4.1.min.js')
            ->prependFile($this->basePath() . '/js/bootstrap.min.js')
            ->prependFile($this->basePath() . '/js/jquery-1.10.2.min.js')
        ; ?>

        <title>Test</title>
    </head>
    <body data-spy='scroll' data-target='#header' data-offset='200'>


        <?php require($this->basePath() . 'temp-old/views/shared/header.php') ?>

        <div id='content'>
            <?php echo $this->content; ?>

            <?php require($this->basePath() . 'temp-old/views/shared/footer.php') ?>
        </div>
    </body>
</html>

cv.phtml

<h1 class='heading-1 small-separator'>
    // My heading
</h1>
<section>
    // All my CV
</section>

index.phtml

<!-- Presentation -->
<?php require('_presentation.php'); ?>
<!-- End Presentation -->

<!-- Prestations -->
<?php require('_prestations.php'); ?>
<!-- End Prestations -->

<!-- Contact -->
<?php require('_contact.php'); ?>
<!-- End Contact -->

【问题讨论】:

  • 听起来有点奇怪,我认为只是重新安装/更新 ZendSkeletton,然后从那里开始。我从来没有遇到过这样的事情。
  • 如果您使用 ZendFramework 2 骨架应用程序,有一个布局但仍然是空的。您在 (vhost/) 中看到的是来自 index.phtml 的视图脚本。
  • 视图中似乎有错误。确保正在显示错误。
  • 请发布两个视图脚本,以便我们找出错误所在。可能在cv.phtml 你有一个错误。
  • 所以我添加了视图脚本:layout.phtml、cv.phtml 和 index.phtml 我的应用程序是 ZF2 Skeleton Application 的完整复制粘贴,但配置、控制器和视图除外。

标签: php zend-framework zend-framework2


【解决方案1】:

好的,我解决了我的问题,所以它真的是假的(是的,我是)错误:

我已经在 cv.phtml 中添加了一个链接:

<a href='$this.basePath()'>my link</a>

所以每次我尝试同时渲染布局+视图时,视图脚本都会抛出异常: "$this.basePath() 是一个未知函数"

我昨天:“哇哦?!那为什么?我在 index.phtml 中使用了它!”

今天的我:“真是一团糟!PHP 调用对象方法时使用 ->,而不是 '.'。

因此,视图脚本 cv.phtml 没有任何错误,它与我的布局一样工作。

感谢大家的回复,感谢 Jurian Sluiman 确认我的 cv.phtml 可能有错误。

【讨论】:

    猜你喜欢
    • 2015-01-02
    • 2015-01-25
    • 1970-01-01
    • 2023-03-03
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2012-09-20
    相关资源
    最近更新 更多