如何调用默认布局ZF2所有模块或选择的模块?(How to call default layout

2019-10-17 11:18发布

我在模块管理/视图/布局/ dashboard.phtml开发dashboard.phtml

现在,我已经开发了如餐馆的另一个模块现在我想在restarunts模块的所有动作我在下面试图用dashboard.phtml。

我module.config.php

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

    ),

我restaurantsController.php的的indexAction

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

它不工作,请帮助我,如果我叫仪表盘为我的管理端模块我的默认布局将是巨大的堂妹这是不符合逻辑相同dashboard.phtml掌握在我的模块中的所有文件夹。

Answer 1:

是的,我找到了解决办法,我改变我的代码,如下面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');
    });
}

在我的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
    ));

}


文章来源: How to call default layout in ZF2 for all modules or selected modules?