Change layout in the controller of Zend Framework 2.0

前端 未结 5 1098
旧巷少年郎
旧巷少年郎 2020-12-10 00:57

I am learning ZF2.0 beta. In ZF1 we can change layout on the fly in controller:

Zend_Layout::getMvcInstance()->setLayout(\'layoutname\');         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 01:24

    The best way I've found to set templates in actions is like this

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

    In your module.config.php make sure you've set your appropriate template_map path.

        'view_manager' => array(
        'template_map' => array(
            'layout/custom' => __DIR__ . '/../view/layout/custom.phtml'
        ),
    ),
    

提交回复
热议问题