CakePHP网站移动版 [英] CakePHP website mobile version

查看:20
本文介绍了CakePHP网站移动版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 CakePHP 框架开发了一个完整的网站,我们希望为移动设备(主要是 iPhone/iPad)制作一个非常轻便的网站版本.

I have developed a full website with CakePHP framework and we'd like to make a very light version of the website for mobile devices (mainly iPhone/iPad).

有没有办法使用带有新子域(例如 mobile.mywebsite.com)的现有网站来呈现特定视图?我想避免复制和简化当前的以符合新的要求.我不想每次需要更改控制器操作时都必须重新开发"一个新的 CakePHP 网站并进行两次更改.

Is there a way to use the existing website with a new sub domain (for instance mobile.mywebsite.com) which will render specific views? I would like to avoid copying and simplifying the current one to match the new one requirements. I do not want to have to "re-develop" a new CakePHP website and do the changes twice every time I need to change a controller action.

推荐答案

我已经通过对 app_controller.php 文件中的 beforeFilter() 快速添加来完成此操作.

I've done this using a quick addition to the beforeFilter() in my app_controller.php file.

function beforeFilter() {
     if ($this->RequestHandler->isMobile()) {
        $this->is_mobile = true;
        $this->set('is_mobile', true );
        $this->autoRender = false;
     }
}

这使用 CakePHP RequestHandler 来检测它是否是访问我网站的移动设备.它设置了一个属性和视图变量,以允许视图根据新布局调整自身.还会关闭自动渲染,因为我们会在 afterFilter 中处理它.

This uses the CakePHP RequestHandler to sense if it's a mobile device visiting my site. It sets a property and view variable to allow actions an views to adjust themselves based on the new layout. Also turns off the autoRender because we'll take care of that in an afterFilter.

在 afterFilter() 中,它查找并使用移动视图文件(如果存在).移动版本存储在控制器视图文件夹内的移动"文件夹中,其名称与普通非移动版本完全相同.(即 add.ctp 变成 mobile/add.ctp)

In the afterFilter() it looks for and uses a mobile view file if one exists. Mobile versions are stored in a 'mobile' folder inside the controller's view folder and are named exactly the same as the normal non-mobile versions. (ie. add.ctp becomes mobile/add.ctp)

    function afterFilter() {
        // if in mobile mode, check for a valid view and use it
        if (isset($this->is_mobile) && $this->is_mobile) {
            $view_file = new File( VIEWS . $this->name . DS . 'mobile/' . $this->action . '.ctp' );
            $this->render($this->action, 'mobile', ($view_file->exists()?'mobile/':'').$this->action);
        }
     }

这篇关于CakePHP网站移动版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆