views:

59

answers:

2

Hi,
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).

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.

A: 

Yes, you can re use all of your domain and controllers, have a look to Tera-WURLF

And even better, you don't need a subdomain for the mobile version.

mcabral
Hi mcabral, thank you for your suggestion. I really like the idea of a sub domain because people could then switch to the full site if they want. Any idea on this feature? Have you ever used it? Cheers,
Nicolas
@Nicolas yes, i have tried it with Zend Framework. It's a fine tool.
mcabral
+2  A: 

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

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

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.

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);
        }
     }
Dan Berlyoung
Hi Dan, do you think it would be possible use your solution with a sub-domain and then let the users run the full website via a link if he wants to? Cheers,
Nicolas
Possibly. My solution autosenses which device is browsing. Maybe just add a link that sets a cookie to override the browser sensing. Then no subdomain is necessary. Just a 'view full site' link that sets the cookie.
Dan Berlyoung
The cookie could be a good idea indeed. I'll think about it this weekend, but it is most likely that your easy to configure solution will have my vote. Cheers,
Nicolas
So I'm back, just to tell you I choose your option. I just needed to add a meta "viewport" in order to make the same layout on all mobile devices. Thanks! Nicolas.
Nicolas