Zend Framework 2:仅在索引操作上呈现布局

时间:2013-09-09 17:52:34

标签: php zend-framework zend-framework2

我是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个控制器,使用子路由,但这些都没有工作......

有人可以告诉我我的代码有什么问题吗?

谢谢, McSIME

编辑:

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

昨天我:“Whaaaaat?!为什么?我用它进入index.phtml!”

我今天:“多么糟糕!PHP使用 - &gt;来调用对象方法,而不是'。'。

因此视图脚本cv.phtml没有任何错误,它可以按照我的布局工作。

感谢所有回复和Jurian Sluiman确认我的cv.phtml可能有错误。