Home > tools, user > Theme Your CakePHP Application – Make it Mobile Device Aware

Theme Your CakePHP Application – Make it Mobile Device Aware

July 26th, 2010

This post shows how easy your CakePHP applications becomes mobile device aware by using CakePHP theming feature. At the end of this post the mobile theme is selected automatically on requests of mobile devices.

Theming in CakePHP is really a piece of cake! Since CakePHP is a Model-View-Control framework, the data, the data logic and the view is well separated. For theming you just need to replace your view templates without changing the logic or data handling. Theming is quite easy and you don’t need to be a professional software programmer. While a nice theme needs some knowledge of HTML and CSS theming CakePHP requires just basic PHP skills.

CakePHP’s Layout and Views

In standard cases all view templates are located in ./app/views and CakePHP selects by its magic the correct page layout and the view for the current logic. So you have your layouts (the basic document structure) in ./app/views/layouts and your view templates (the specific view of one action) of your current action in ./app/views/[controller]/[action].ctp. For detailed information please have a look at http://book.cakephp.org/de/view/94/Views.

The CSS and JS files have a little different place. The style sheets are located in ./app/webroot/css and the javascript files are located in ./app/webroot/js.

The important structure of CakePHP’s view templates is shown below:

./app/views/
    views/
        layouts/           <-- Location of page layouts
            default.ctp
        helpers/           <-- View template helpers
        elements/          <-- Little view templates
        home/
            index.ctp      <-- Specific view template of the index action in the home controller
        explorer/
            index.ctp
        ...
        /themed            <-- Directory of CakePHP themes
    webroot/               <-- Application's webroot (like htdocs for apache)
        css/               <-- Style sheets
        js/                <-- Javascript directory

Now the cool part: You can theme all your views with a very similar view structure. But you don’t have to theme all layouts and views. If your theme does not have a required view CakePHP uses the original one. So you can just theme required layouts and/or views.

The Birth of a New Theme

Now we want to create a mobile version of our application. As example application I use the open source social photo gallery phTagr from www.phtagr.org. I assume that your phTagr gallery is installed and accessible at http://localhost/phtagr. But you can easily adapt these steps to any other CakePHP application.

Basic Folder Structure of a CakePHP Theme

First we need to create a theme folder with its basic theme structure. Goto theme directory ./app/views/themed and create your own theme, I call it “mobile”. Further some required directories are also required. We theme the default layout and some basic actions, for phTagr it is the home, the photo explorer and the image view.

cd app/views/
mkdir -d themed/mobile
cd themed/mobile
mkdir -p layouts elements webroot/css home explorer images

Now we have following folder structure

./app/views/themed
    /mobile
        layouts/           <-- Default theme layouts
        elements/          <-- Folder for element templates
        webroot/
            css/           <-- Folder for mobile CSS
        home/              <-- Your themed views of home controller
        explorer/          <-- Your themed views of explorer controller
        images/            <-- Your themed views of images controller

Note: ./app/views/themed/mobile is now referred as ./mobile.

Activate Your Theme

To activate the new theme you set it in before_render() at ./app/app_controller.php. Later we automate this theme selection but for now the hard coded version is just fine. Important is, that the views are theme aware by this->view = 'Theme'; and of cause your theme name.

  function beforeRender() {
    // ... other code
    $this->view = "Theme";
    $this->theme = "mobile";
  }

Your Themed Page Layout

If we call now our side http://localhost/phtagr nothing is changed. Thats fine, than CakePHP did not find any themed layouts or views to use and uses the standard one. Therefore we want to change the default layout in ./mobile/layouts/default.ctp to see some differences.

Edit ./mobile/layouts/default.ctp

<?php echo $html->docType('xhtml-strict'); ?>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title><?php echo $title_for_layout; ?></title>
<?php
  echo $html->charset('UTF-8');
  echo $html->meta('icon');
?>
</head>

<body>
<?php echo $content_for_layout; ?>
</body>
</html>

Now we see some changes http://localhost/phtagr with a plain layout.

Layout Your Views

My profession is not quite a web designer but here I show you howto style your mobile version.

You create your css in ./mobile/webroot/css/mobile.css and add it to your default theme in the <head> section.

...
<?php
  echo $html->charset('UTF-8');
  echo $html->meta('icon');
  echo $html->css('mobile');
?>
...
* {
  margin: 0;
  padding: 0;
}

body {
  width: 100%;
  color: black;
  font-family: Druid, Verdana, Sans;
  font-size: 10pt;
  background: white;
}

h1,h2,h3 { font-weight: bold; }
h1 { font-size: 130%; }
h2 { font-size: 120%; }
h3 { font-size: 110%; }

a { text-decoration: none; }
a img { border: 1px black solid; }

Add Header and Footer

Now we want to add some containers for header and footer. These parts could be rendered in special containers, CakePHP calls them elements template. Header and footer are perfect candidates for such elements.

Adapt your default HTML body layout in ./mobile/layouts/default.ctp

<body>
<div><?php echo View::element('header'); ?></div>
<div><?php echo $content_for_layout; ?></div>
<div><?php echo View::element('header'); ?></div>
</body>

Now create the elements ./mobile/elements/header.ctp and ./mobile/elements/footer.ctp.

header.ctp:

<h1>phTagr<span>mobile</span></h1>

footer.ctp:

<p>Social Web Gallery <a href="http://www.phtagr.org">phTagr</a> - mobile version.</p>

And we add new styles to ./mobile/webroot/css/mobile.css:

.header * {
  display: inline;
}
.header {
  display: block;
  background: #3d3;
  padding: 2px 5px;
}
.header h1 span.subheader {
  color: white;
  font-size: 80%;
  font-style: italic;
}
.footer {
  display: block;
  background: #888;
}

Adapt Views

Now some views will be adapted from the original ones. The original versions are located in ./app/views/[controller]/. In the theme they are located in ./app/themed/[theme]/[controller], in ./mobile/[controller].

For a theme view of the index action of the home controller CakePHP will look first in ./mobile/home/index.ctp. If it does not exists CakePHP take the standard one. So I copied the original version index.php to the theme folder ./mobile/home and made some changes.

Further I copied and adapt ./app/views/explorer/index.ctp and ./app/views/images/index.ctp. All other themes are not very important to adapt. The same with the view action of images controller.

For the forms I added some basic form style definitions to ./mobile/webroot/css/mobile.css:

fieldset {
  margin: 5pt;
  border: none;
}
fieldset legend {
  font-weight: bold;
}
fieldset div label {
  display: block;
  width: 100%;
}
fieldset div input[type=textfield],
fieldset div input[type=text] {
  width: 100%;
  font-size: 150%;
}
input[type=checkbox] + label {
  display: inline;
}
form * input[type=submit] {
  font-size: 150%;
  border: 2px solid black;
  background: #3d3;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
}

And styled the flag message:

.message {
  display: block;
  font-size: 120%;
  margin: 5pt;
  padding: 5pt;
  border: 1px solid black;
  background: #fd3;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
}

Fix iPhone

The iPhone (and iPod Touch) requires a special viewport meta information to render without any scaling. This is added to the default layout in the head section:

<title><?php echo $title_for_layout; ?></title>
<meta name="viewport" content="initial-scale=1.0">
...

Automatic Theme Selection

After adjusting the views and improving the style sheets the automatic theme switch is build in. For this we need the RequestHandler component in our basic app_controller.php which is added to the $components variable:

  var $components = array('RequestHandler');

We use the isMobile() function of RequestHandler component to evaluate the client device type and select the mobile theme for mobile devices

  function beforeRender() {
    // ... other code
    if ($this->RequestHandler->isMobile()) {
      $this->view = "Theme";
      $this->theme = "mobile";
    }
  }

Summary

The theme feature of CakePHP is straight forward and easy to use. You can customize your CakePHP application easily by changing the page layout or partial theming by writing single view templates of specific controller actions. The example showed how easy your application becomes mobile device aware.

You can see the full mobile theme of phTagr at http://trac.phtagr.org/browser/trunk/views/themed/mobile.

Categories: tools, user Tags: ,
  1. m16u31
    May 28th, 2011 at 04:28 | #1

    hi, thank u 4 this tutorial, it was very helpfull.

    but i need to do something, i need to switch the mobile version to standar version and
    standar version to mobile version.

    any idea??

  2. August 10th, 2011 at 07:42 | #2

    I’m very sorry for this late reply. Your comment landed accidentally in spam.

    If you need to switch explicitly between mobile and standard version you can save the current version in the HTTP session. The user might switch between the version through an special URL parameter which is checked in app_controller.php and the version is stored for further requests. If no parameter is given, the default behavior is used.

    Xemle

  3. Pouria
    September 18th, 2011 at 21:16 | #3

    could you re-up the demo, please?

  4. September 19th, 2011 at 11:22 | #4

    @Pouria: The demo gallery at http://demo.phtagr.org is up and running with mobile support. You can access the mobile version with your mobile client. Otherwise check browser extensions to change your user agent information to an mobile client.

    The source code is now hosted at GitHub at https://github.com/xemle/phtagr/tree/master/views/themed/mobile

Comments are closed.