如何在 ZF2 中为所有模块或选定模块调用默认布局? [英] How to call default layout in ZF2 for all modules or selected modules?

查看:20
本文介绍了如何在 ZF2 中为所有模块或选定模块调用默认布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的模块 Admin/view/layout/dashboard.phtml 中开发了dashboard.phtml

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

现在我已经开发了另一个模块,比如餐厅,现在我想在下面尝试的 restarunts 模块的所有操作中使用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.

我的 module.config.php

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',

    ),

我对restaurantController.php 的索引操作

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

如果我将仪表板称为我的管理端模块的默认布局,它不起作用请帮助我,这会很棒,因为它不是逻辑相同的dashboard.phtml 位于我模块的所有文件夹中.

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.

推荐答案

是的,我找到了解决方案 我在 module.php 中更改了如下代码

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

更改我的 indexAction.

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

}

这篇关于如何在 ZF2 中为所有模块或选定模块调用默认布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆