Zend框架,具有默认和移动布局


Zend framework with default and mobile layout

目前我有一个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中有两个布局,我可以使用一些控制器功能,这样它就可以用于移动布局吗?我在引导中也有布局加载器

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

我现在应该在哪里调整,这样我就可以有一个控制器类和两个单独的布局。谢谢

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

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