如何在zf2中包含文件phtml

I'm using Zend Framework 2. And i want to include file phtml to other phtml. Here's my folder structure :

::module
  ->AppFeeder
    ->config
    ->src
      ->AppFeeder
        ->Controller
          ->FeederController.php
        ->view
          ->app-feeder
            ->feeder
              ->index.phtml
            ->layout
              ->menu.phtml

I want index.phtml include file menu.phtml. How to include that file ? Can you help me ?

From you controller actions you can include the .phtml as a child view like this.

public function indexAction() {
   // This will by default use the index.phtml file from the feeder directory
   $view = new ViewModel();

   $menu = new ViewModel();
   $menu->setTemplate('app-feeder/layout/menu.phtml');

   $view->addChild($menu, 'menu');

   return $view;
}

Then you can include it in your index.phtml file using

<?php echo $this->menu; ?>

You have 3 ways:

  • partials
  • child views
  • view helpers

Anyway I recommend you renaming layout folder to partial. Layout is the template for all your views and it has an special treatment in zf2.