Php 在ZF2中添加了模板映射动态

Php 在ZF2中添加了模板映射动态,php,zend-framework2,Php,Zend Framework2,我必须硬代码添加新的布局。然后我想找到一些方法在ZF2中添加模板映射动态 My module.config.php 'view_manager' => array ( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'excep

我必须硬代码添加新的布局。然后我想找到一些方法在ZF2中添加模板映射动态

My module.config.php

'view_manager' => array (
    'display_not_found_reason' => true,
    'display_exceptions' => true,
    'doctype' => 'HTML5',
    'not_found_template' => 'error/404',
    'exception_template' => 'error/index',
    'template_map' => array (
            'layout/layout' => __DIR__ . '/../../../template/layout/layout.phtml',
            'layout/custom' => __DIR__ . '/../../../template/layout/custom.phtml',
            'error/404' => __DIR__ . '/../../../template/error/404.phtml',
            'error/index' => __DIR__ . '/../../../template/error/index.phtml' 
    ),
    'template_path_stack' => array (
            __DIR__ . '/../view/'
    ) 
) 
'template_path_stack' => array (
     __DIR__ . '/../view/',
 __DIR__ . '/../../../' //Parent folder of template path
) 
我用这种方式设置了新的布局

$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
    $controller = $e->getTarget();
    $controller->layout('template_name');
}, 100);
请给我一些建议/样品

谢谢

==================

2012年8月12日更新:

我找到了解决方案并应用到我的“层次模板系统”

修改module.config.php

'view_manager' => array (
    'display_not_found_reason' => true,
    'display_exceptions' => true,
    'doctype' => 'HTML5',
    'not_found_template' => 'error/404',
    'exception_template' => 'error/index',
    'template_map' => array (
            'layout/layout' => __DIR__ . '/../../../template/layout/layout.phtml',
            'layout/custom' => __DIR__ . '/../../../template/layout/custom.phtml',
            'error/404' => __DIR__ . '/../../../template/error/404.phtml',
            'error/index' => __DIR__ . '/../../../template/error/index.phtml' 
    ),
    'template_path_stack' => array (
            __DIR__ . '/../view/'
    ) 
) 
'template_path_stack' => array (
     __DIR__ . '/../view/',
 __DIR__ . '/../../../' //Parent folder of template path
) 
在Module.php中添加了:

$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
    $controller = $e->getTarget();
    $controllerClass = get_class($controller);


    //Get routing info
    $controllerArr = explode('\\', $controllerClass);
    $currentRoute = array(
        'module' =>  strtolower($controllerArr[0]),
        'controller' => strtolower(str_replace("Controller", "", $controllerArr[2])),
        'action' => strtolower($controller->getEvent()->getRouteMatch()->getParam('action'))
    );


    //Get curr route
    $currAction = implode('/',$currentRoute);
    $currController = $currentRoute['module'] . '/' . $currentRoute['controller'];
    $currModule = $currentRoute['module'];



    //Template file location
    $templatePath = __DIR__ .'/../../template/';

    //Set template
    $template = 'layout/layout'; // Default template

    if (file_exists($templatePath . $currAction.'.phtml')) {
        $template = $currAction;
    }else if(file_exists($templatePath . $currController.'.phtml')) {
        $template = $currController;
    }else if(file_exists($templatePath . $currModule.'.phtml')) {
        $template = $currModule;
    }else{
        if($currentRoute['controller']=='admin'){
            $template = 'admin/layout'; // Admin default template
        }
    }

    $controller->layout('template/'.$template); //Pevert duplicate layout
}, 100);

注意:如果在“布局”和“视图”之间设置相同的键变量。创建新的viewmodel时,它将呈现重复的“布局”,并且不理解控制器中的当前视图。您可以在其中设置已创建的模板

public function someAction() {
    $viewModel = new ViewModel();
    $viewModel->setTemplate('layout/custom');

    return $viewModel;
}

只需确保layout.phtml文件位于模板映射中设置的路径中

Hi,是否要将“template\u name”设置为变量?比如基于域名/网站的名称?是的。我想基于当前模块/控制器/视图在“模板名称”中创建新行。然后我想找到一些方法来添加它,而不需要module.config.phpThanks中的硬代码!但我的意思是,我想在module.config.phpI中添加新的“template_map”,而不需要硬代码。了解您的需求,我想我正在处理相同的事情。一个应用程序,两个网站使用相同的控制器,但每个网站的布局和视图不同。嗯,不适用于网站,但这也是个好主意。我想要类似“wordpress模板层次结构”的东西。您可以基于当前模块/控制器/操作覆盖模板,所以您正在寻找更多主题引擎类型的方法来更改模板?这会有点棘手。您可以有一个基本的主应用程序布局,然后有一个模板作为主题,并用子模板将您的站点拼凑在一起。