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).
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.
The solution I went with was a lightweight modification based on a few of the answers here, for CakePHP 2.5.5. The handling is all done in the beforeRender (note that beforeRender is only run on controller actions that will actually render a page, so this saves overhead as opposed to beforeFilter/afterFilter for private methods):
The $mobile variable can be used on any view if you have small tweaks to make, otherwise you can optionally replace any view with View/{controller}/mobile/same_file_name.ctp or layout with View/Layouts/mobile/same_file_name.ctp to have a separate page structure entirely.
Note that this uses $this->view and $this->layout and then modifies them, instead of using $this->action and $this->render(view,layout), because your view won't always match your action (same view, multiple actions, for example, break using $this->action), and this solution prevents needing to worry about when $this->render() will be forced, and allows it to happen naturally.
Dan's answer worked for me. However, I used file_exists instead of the File constructor and added the ability to use mobile layouts. The before filter was the same, but the afterFilter looked like this:
You can use Theme feature in CakePHP 2.x for mobile layout.
Simply do:
I found this better, as you can share View file on mobile and desktop theme easily.
I've done this using a quick addition to the beforeFilter() in my app_controller.php file.
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)
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.
CakePHP v2.2.1 Solution (+ Cookies to persist mobile/desktop/other layout)
This solution is based on answers by @Dan Berlyoung, @deewilcox and @Chris K.
Parts of those answers failed to work (for me) in CakePHP 2.2.1.
I also extended the solution to support "forcing" a mobile/desktop/other layout from the frontend - useful for debugging and for users that don't want to be stuck on a "mobile" themed layout.
/app/Controller/AppController.php
/app/View/Elements/default_footer.ctp
/app/View/Layouts/default.ctp
/app/View/Layouts/mobile/default.ctp
/app/View/Pages/home.ctp
/app/View/Pages/mobile/home.ctp
Usage
Use the
default_footer
links to change layout - or these direct urlshttp://example.com/pages/home?forcedLayout=desktop
http://example.com/pages/home?forcedLayout=mobile
A session COOKIE persists the option you've chosen... e.g. try setting to "mobile" and then visit a url without the
forcedLayout=
param.http://example.com/pages/home
The
default_footer
links persist existing params (except for the "fragment" #gohere)http://example.com/pages/home/a/b/c:d?this=that&foo=bar#gohere
Desktop Site url is:
http://example.com/pages/home/a/b/c:d?this=that&foo=bar&forcedLayout=desktop
For more robust device User-Agent detection consider using the Mobile Detect PHP Library ... you could then target Tablets, and even specific devise OS versions.... Oh what fun! ^_^