0

目前我有一个 zend 框架应用程序,我将它与 wurfl 用户代理集成,所以我可以在移动和桌面版本之间切换,我的插件驻留在库中

<?php
 class Zc_Controller_Plugin_TemplatePicker extends Zend_Controller_Plugin_Abstract
  {
protected $useragent;


public function postDispatch(Zend_Controller_Request_Abstract $request)
{
    $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
    $this->useragent = $bootstrap->getResource('useragent');

    if($this->useragent->getDevice()->getType() == 'mobile')
    {
        Zend_Layout::getMvcInstance()->setLayout('mobile');
    }

}
 }

现在我在脚本文件mobile.phtml和layout.phtml中有2个布局,我可以使用一些控制器功能以便它可以用于移动布局,而且我在引导程序中有布局加载器

    protected function _initLayoutHelper()
{
   // $front = Zend_Controller_Front::getInstance();
   // $front->registerPlugin(new Zc_Controller_Plugin_TemplatePicker());  
    if(!stristr($_SERVER['REQUEST_URI'], '/admin')){
        $this->bootstrap('frontController');
    }
    $layout = Zend_Controller_Action_HelperBroker::addHelper(new Zc_Controller_Action_Helper_LayoutLoader());
}

并且布局加载器代码是

<?php

class Zc_Controller_Action_Helper_LayoutLoader extends   
  Zend_Controller_Action_Helper_Abstract
 {
public function preDispatch(){

    $bootstrap = $this->getActionController()->getInvokeArg('bootstrap');
    $config = $bootstrap->getOptions();
    Zend_Registry::set('config', $config);
    $module = $this->getRequest()->getModuleName();
    $controller = $this->getRequest()->getControllerName();
    $action = $this->getRequest()->getActionName();

    $layoutScript = "layout";
    if (isset($config[$module]['resources']['layout']['layout'])) {
        $layoutScript = $config[$module]['resources']['layout']['layout'];
    }
    $this->getActionController()->getHelper('layout')->setLayout($layoutScript);
}
 }

我现在应该在哪里调整,以便我可以拥有 1 个具有 2 个单独布局的控制器类。谢谢!

4

1 回答 1

1

如果您希望为某个控制器设置某些特定布局,您可以使用以下代码:

class Custom_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch()
    {
        $front = Zend_Controller_Front::getInstance();
        $layout = Zend_Layout::getMvcInstance();

        switch ($front->getRequest()->getControllerName()) {
            case "index":
                $layout->setLayout('home');
                break;
            case "login":
                $layout->setLayout('login');
                break;
            default:
                $layout->setLayout('default');
        }
    }

}
于 2012-07-12T07:54:05.170 回答