简体   繁体   中英

How to call default layout in ZF2 for all modules or selected modules?

I have developed dashboard.phtml in my module Admin/view/layout/dashboard.phtml

Now I have developed another module like restaurants now I want to use dashboard.phtml in all action of restarunts module I tried below.

my module.config.php

 'view_manager' => array(
    'template_map' => array(
        'layout/default' => __DIR__ . '/../../Admin/view/layout/dashboard.phtml'
    ),
    'template_path_stack' => array(
        'restaurants' => __DIR__ . '/../view',

    ),

my indexaction of restaurantsController.php

public function indexAction()
{

   $viewModel = new ViewModel();
   $viewModel->setTemplate('layout/default');
   $restaurant_info_array = array();
   $restaurant_info = $this->getRestaurantsTable()->fetchAll();
    $i = 0;
    foreach ($restaurant_info as $ri)
    {
        $restaurant_info_array[$i]['id'] = $ri->id;
        $restaurant_info_array[$i]['name'] = $ri->name;
        $restaurant_info_array[$i]['published'] = $ri->published;
        $restaurant_info_array[$i]['email'] = $ri->email;
        $is_menu_available = $this->getRestaurantsTable()->getRestaurantsMenu($ri->id);
        $restaurant_info_array[$i]['res_menu'] = count($is_menu_available);
        $i = $i+1;
    }

    $viewModel->setVariables(array(
            "restaurants"=>$restaurant_info_array
    ));

it is not working please help me if I call dashboard as my default layout for my admin side modules it will be great coz It is not logical same dashboard.phtml lies in all folders of my modules.

Yes I found the solution I change my code as below in module.php

public function onBootstrap(MvcEvent $e)
{ 
    $e->getApplication()->getServiceManager()->get('translator');
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
   //added lines for layout
    $eventManager->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
    $controller = $e->getTarget();
    $controller->layout('layout/default');
    });
}

change in my indexAction.

public function indexAction()
{
   $viewModel = new ViewModel();
   $restaurant_info_array = array();
   $restaurant_info = $this->getRestaurantsTable()->fetchAll();
   $i = 0;
   foreach ($restaurant_info as $ri)
   {
        $restaurant_info_array[$i]['id'] = $ri->id;
        $restaurant_info_array[$i]['name'] = $ri->name;
        $restaurant_info_array[$i]['published'] = $ri->published;
        $restaurant_info_array[$i]['email'] = $ri->email;
        $is_menu_available = $this->getRestaurantsTable()->getRestaurantsMenu($ri->id);
        $restaurant_info_array[$i]['res_menu'] = count($is_menu_available);
        $i = $i+1;
    }

    return $viewModel->setVariables(array(
            "restaurants"=>$restaurant_info_array
    ));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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