如何在ZF2中为所有模块或所选模块调用默认布局?

时间:2012-12-12 10:37:25

标签: php zend-framework2

我在我的模块Admin / view / layout / 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
    ));

它不起作用请帮助我如果我将仪表板作为我的管理员模块的默认布局,它会很棒coz它不合逻辑同样dashboard.phtml位于我模块的所有文件夹中。

1 个答案:

答案 0 :(得分: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
    ));

}