title: How to change layout in controller in ZendFramework2?
body: <p>I found this topic and answer: <a href="https://stackoverflow.com/q/9112766/2898694">Change layout in the controller of Zend Framework 2.0</a> :: <a href="https://stackoverflow.com/a/9413957/2898694">Answer</a></p>
  
  <p>I am trying to do this:</p>
  
  <pre><code>public function loginAction() {
      if ($this-&gt;zfcUserAuthentication()-&gt;hasIdentity()) {
          return $this-&gt;redirect()-&gt;toRoute('zfcadmin');
      }
      $this-&gt;layout('layout/login');
      return new ViewModel();
  }
  </code></pre>
  
  <p>But it doesn't work. </p>
  
  <p>Sure I have file <code>MODULE_DIR/view/layout/login.phtml</code>. </p>
  
  <p>I tried to <code>var_dump($this-&gt;layout());</code> before setting layout and after it and it shows, that layout is changed after <code>$this-&gt;layout('layout/login');</code> line. But it is not.</p>
  
  <p><strong>How to set different layout in controller?</strong></p>
  
  <p>Also, <strong>why I don't get any messages if layout is changed</strong>? <strong>Why standart layout loaded, instead of error</strong>? </p>
  
  <p>I think, I have to set up layout somewhere (like I set routes, for example). Possibly in config <code>['view_manager']['template_map']</code> by adding something like:</p>
  
  <pre><code>$config = array(
      'view_manager' =&gt; array(
          'template_path_stack' =&gt; array(
              __DIR__ . '/../view'
          ),        
          'template_map' =&gt; array(
              'layout/login'           =&gt; __DIR__ . '/../view/layout/login.phtml',
          ),
      ),
  );
  </code></pre>
  
  <p>— like said <a href="https://stackoverflow.com/a/19573445/2898694">there</a>:</p>
  
  <blockquote>
    <p>Of course you need to define those layouts, too... just check
    Application Modules <code>module.config.php</code> to see how to define a layout.</p>
  </blockquote>
  
  <p>That didn't helped me :(</p>
  
  <h2>Update 1</h2>
  
  <p>Tried this:</p>
  
  <pre><code>public function loginAction() {
      if ($this-&gt;zfcUserAuthentication()-&gt;hasIdentity()) {
          return $this-&gt;redirect()-&gt;toRoute('zfcadmin');
      }
      $layout = $this-&gt;layout();
      $layout-&gt;setTemplate('layout/login');
      return new ViewModel();
  }
  </code></pre>
  
  <p>as <a href="https://stackoverflow.com/users/3622039/alex">@alex</a> suggested. Doesn't work :'(. Same result without <code>return new ViewModel();</code> line.</p>
  
  <p>You review files by yourself:</p>
  
  <ul>
  <li><a href="https://github.com/sharikovvladislav/my-zf2-project/blob/developing/module/MyZfcAdmin/src/MyZfcAdmin/Controller/AdminController.php" rel="nofollow noreferrer">AdminController.php</a> (loginAction)</li>
  <li><a href="https://github.com/sharikovvladislav/my-zf2-project/blob/developing/module/MyZfcAdmin/config/module.config.php" rel="nofollow noreferrer">module.config.php</a> (to be sure I added <code>layout/login</code> correctly</li>
  </ul>
  
  <h2>Update 2</h2>
  
  <p>I tried to debug as you suggest. </p>
  
  <p>I updated <code>__invoke</code> functioN:</p>
  
  <pre><code>public function __invoke($template = null)
  {
      var_dump($template);
      die();
      if (null === $template) {
          return $this-&gt;getViewModel();
      }
      return $this-&gt;setTemplate($template);
  }
  </code></pre>
  
  <p>There are some cases: </p>
  
  <p>With code, you suggested:</p>
  
  <pre><code>$layout = $this-&gt;layout();
  $layout-&gt;setTemplate('layout/login');
  </code></pre>
  
  <p>it displays NULL. So method is called, but <code>$template</code> is null variable.</p>
  
  <p>With code, from <a href="https://stackoverflow.com/a/9413957/2898694">post</a>, I had given in the start of my post:</p>
  
  <pre><code>$this-&gt;layout('layout/login');
  return new ViewModel();
  </code></pre>
  
  <p>It shows <code>string(12) "layout/login"</code>.</p>
  
  <p>Without any code (so layout <code>layout/admin</code> loaded (default for <code>ZfcAdmin</code>), it shows: <code>string(12) "layout/admin"</code>.</p>
  
  <p>If I load <code>/</code> of my site it page is loaded with standart layout (in both cases with or without <code>layout/layout</code> in module config.</p>
  
  <h2>Update 3</h2>
  
  <p>I tried this:</p>
  
  <pre><code>$layout = $this-&gt;layout();
  var_dump($layout-&gt;getTemplate());
  $layout-&gt;setTemplate('layout/login');
  var_dump($layout-&gt;getTemplate());
  die();
  </code></pre>
  
  <p>in controller. It shows: <code>string(13) "layout/layout" string(12) "layout/login"</code>. So layout is changed. But standart layout <code>layout/layout</code> rendered instead of <code>layout/login</code>. :(</p>