CakePHP网站手机版 [英] CakePHP website mobile version

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

问题描述



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


I have developped 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.



Cheers,

Nicolas。


Cheers,
Nicolas.

推荐答案

到我的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来检测是否是移动设备访问我的网站。它设置一个属性和视图变量,以允许动作视图根据新布局调整自己。还会关闭autoRender,因为我们会在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天全站免登陆