2

我创建了一个 Zend Framework 网站,现在我正在更新它以根据用户是否在移动设备上切换布局文件。

我已经编写了一个类来处理检测,但我不知道在哪里最好放置这个检查并触发正在使用的布局文件。

代码:

include(APPLICATION_PATH . "/classes/MobileDetection.php");
$detect = new MobileDetect();

if ($detect->isMobile()) {
    $layout = $layout->setLayout('mobile');
} 

我可以从 Bootstrap 函数触发布局,_initViewHelpers()但只要我在上面添加包含行,就会收到 500 错误。

关于如何以及在哪里放置它的任何建议?我最初有一个处理检查的助手,但它用于布局本身,而不是让我能够交换整个布局文件。

4

3 回答 3

2

您可以使用插件,这就是我所做的:

<?php

class Mobile_Layout_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        switch ($request->getModuleName()) {
            case 'mobile': $this->_moduleChange('mobile');
        }
    }

    protected function _moduleChange($moduleName) {
        $this->getLayout()->setLayoutPath(
            dirname(dirname(
                $this->getLayout()->getLayoutPath()
            ))
            . DIRECTORY_SEPARATOR . 'layouts/scripts/' . $moduleName
        );
        $this->getLayout()->setLayout($moduleName);
    }

}

我把它保存在library/ProjectName/Layout/Controller/Plugin/Layout.php.

在您的 Bootsrap 中,您需要合并以下内容:

Zend_Layout::startMvc(
    array(
        'layoutPath' => self::$root . '/application/views/layouts/scripts',
        'layout' => 'layout',
        'pluginClass' => 'Mobile_Layout_Controller_Plugin_Layout'
    )
);

实际上,我花了一段时间才弄清楚这一点,但是一旦你完成了它,你会快乐。希望有帮助:)

于 2011-01-07T00:19:18.693 回答
0

实际上真正发生的是,你有一个新的独立模块,称为“mobile”,布局插件助手实际上正在执行 preDispatch() 方法检查这是否是被调用的模块。之后,该方法正在更改布局。这很复杂。我认为您实际上可以为您的移动版本制作一个基本控制器,并在其 init() 方法中使用 $this->_helper->layout->changeLayout() 更改布局。

于 2011-01-10T15:53:45.830 回答
0

假设您有 www.example.com,当您使用移动设备访问此页面时,您希望被重定向到 mobile.example.com:

知道 www 是一个模块,而 mobile 是应用程序中具有不同布局的模块

我发现以下关于如何检测移动设备的页面http://framework.zend.com/manual/de/zend.http.user-agent.html#zend.http.user-agent.quick-start

如何以及在哪里重定向?

问候

于 2011-02-09T21:45:04.783 回答