Zend Framework 2 -显示正确视图有问题,总是显示layout/layout. php


Zend Framework 2 - having trouble displaying the right view, always displaying layout/layout.phtml

我是Zend Framework 2的新手。

我有几个模块,当用户启动应用程序时,我想显示RecruitCore模块,这就是为什么我目前唯一的路由是'route' => '/',这是我的模块。config。php,用于RecruitCore:

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend'Mvc'Router'Http'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'RecruitCore'Controller'Index',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
             'RecruitCore'Controller'Index' => 'RecruitCore'Controller'IndexController',
        ),
    ),
   'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        //'not_found_template'       => 'error/404',
        //'exception_template'       => 'error/index',
        'template_path_stack' => array(
          __DIR__ . '/../view',
        ),
    ),
);

我有一个IndexController在src/RecruitCore/Controller/IndexController与方法indexAction被调用,因为我的调试消息被解雇。而不是显示视图/recruit-core/index/index。布局,它总是显示layout/layout. PHTML。有趣的是,当我删除view/recruit-core/index/index。PHTML然后我得到一个错误消息,但除此之外,没有其他确认view/recruitment -core/index/index. PHTML

所以我可能在module。config。php
中遗漏了一些东西

Zend Framework使用两步布局。首先,对于控制器动作,呈现视图。这是完整html文档的"内部部分"。然后渲染一个布局。这是另一个视图脚本,它是html文档的"外部部分"。

例如,布局可能像这样:

<html>
  <head>
    <title><?= $this->headTitle() ?>
  </head>
  <body>
    <div class="wrapper">
      <?= $content ?>
    </div>
  </body>
</html>

视图脚本可以像这样:

<h1>Hello world!</h1>
<p>Lorem ipsum</p>

视图的内容被注入到布局的$content变量中。

那么现在更多的信息关于你的想法。默认情况下,ZF2将layout/layout.phtml呈现为布局。对于视图脚本,它是顶级模块名、控制器名和操作方法的组合。

您的模块名为RecruitCore,因此第一个目录名为recruit-core。您的控制器名为IndexController,因此第二个目录名为index。你的方法是indexAction,所以最后你的视图脚本解析为recruit-code/index/index

基本上,你问的是ZF2的正确行为。这在ZF2手册中也有解释。如果愿意,可以重写视图脚本和布局名称。