简体   繁体   English

Zend Framework 2-无法显示正确的视图,始终显示layout / layout.phtml

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

I am new to Zend Framework 2. 我是Zend Framework 2的新手。

I have several Modules, when a user starts the application I want to display the RecruitCore module, thats why my only route at the moment is 'route' => '/', so this is my module.config.php for RecruitCore: 我有几个模块,当用户启动应用程序时,我想显示RecruitCore模块,这就是为什么我目前唯一的路由是'route'=>'/',所以这是我的RecruitCore的module.config.php:

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',
        ),
    ),
);

I have an IndexController in src/RecruitCore/Controller/IndexController with a method indexAction which gets called, because my debug messages are getting fired. 我在src / RecruitCore / Controller / IndexController中有一个IndexController,它带有被调用的方法indexAction,因为我的调试消息被触发了。 But instead of displaying the view/recruit-core/index/index.phtml layout, it always displays the layout/layout.phtml. 但是,它始终显示layout / layout.phtml,而不是显示view / recruit-core / index / index.phtml布局。 The funny thing is, when I delete the view/recruit-core/index/index.phtml then i am getting an error message, but other then that, there is no other acknowledgment of view/recruit-core/index/index.phtml 有趣的是,当我删除view / recruit-core / index / index.phtml时,我收到一条错误消息,但除此之外,没有其他对view / recruit-core / index / index.phtml的确认。

so maybe I am missing something in module.config.php 所以也许我在module.config.php中缺少了一些东西

Zend Framework uses a two-step layout. Zend Framework使用两步布局。 First, for a controller action a view is rendered. 首先,对于控制器动作,呈现一个视图。 This is the "inner part" of the complete html document. 这是完整html文档的“内部”。 Then a layout is rendered. 然后呈现布局。 This is another view script and it's the "outer part" of the html document. 这是另一个视图脚本,它是html文档的“外部”。

For example, a layout might look like this: 例如,布局可能如下所示:

<html>
  <head>
    <title><?= $this->headTitle() ?>
  </head>

  <body>
    <div class="wrapper">
      <?= $content ?>
    </div>
  </body>
</html>

A view script can look like this: 视图脚本如下所示:

<h1>Hello world!</h1>

<p>Lorem ipsum</p>

The content of the view is injected into the $content variable of the layout. 视图的内容被注入到布局的$content变量中。

So now some more info about your idea. 现在,有关您的想法的更多信息。 By default, ZF2 renders the layout/layout.phtml as layout. 默认情况下,ZF2将layout/layout.phtml呈现为布局。 For view scripts, it is a combination of the top-level module name, the controller name and the action method. 对于视图脚本,它是顶级模块名称,控制器名称和操作方法的组合。

Your module is called RecruitCore so the first directory is named recruit-core . 您的模块名为RecruitCore因此第一个目录名为RecruitCore recruit-core Your controller is called IndexController so the second directory is named index . 您的控制器称为IndexController因此第二个目录名为index Your method is the indexAction so finally your view script resolves to recruit-code/index/index . 您的方法是indexAction因此最终您的视图脚本解析为recruit-code/index/index

Basically, what you ask is the correct behaviour for ZF2. 基本上,您要问的是ZF2的正确行为。 This is also explained in the ZF2 manual . ZF2手册对此也进行了说明。 You can override the view script and layout name, if you want to. 如果需要,可以覆盖视图脚本和布局名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM