CakePHP网站手机版

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