0
votes

I am new to 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:

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. But instead of displaying the view/recruit-core/index/index.phtml layout, it always displays the layout/layout.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

so maybe I am missing something in module.config.php

1
Your question is a little confusing. By default, views should be displayed within the layout, so it's normal for the layout to always be displayed. Your layout should be echoing $this->content somewhere in it (this is where the view is shown) - do you have such a line in your layout? See the layout in the ZF2 skeleton for an example: github.com/zendframework/ZendSkeletonApplication/blob/master/…Tim Fountain

1 Answers

0
votes

Zend Framework uses a two-step layout. First, for a controller action a view is rendered. This is the "inner part" of the complete html document. Then a layout is rendered. This is another view script and it's the "outer part" of the html document.

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.

So now some more info about your idea. By default, ZF2 renders the layout/layout.phtml as layout. 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. Your controller is called IndexController so the second directory is named index. Your method is the indexAction so finally your view script resolves to recruit-code/index/index.

Basically, what you ask is the correct behaviour for ZF2. This is also explained in the ZF2 manual. You can override the view script and layout name, if you want to.