CakePHP Ultimate Pages Controller Tips for static website building...

CakePHP is a fantastic web programming framework and a great alternative to Ruby On Rails by which it was inspired. One feature I particularly like which Rails does not have is build in Pages controller used for handling static pages. This feature makes cakephp really suitable for creating mostly static sites with one or two dynamic controllers.

Using sub pages

You can simply place a new file in app/views/pages/ with the extention .thtml and then it would be displayed under http://example.com/pages/ If you need to have a sub folder or sub pages within the pages controller then it is possible to simply create another folder in the pages folder and that would be picked up by the controller. For example create a folder events in pages folder with file 2008.thtml and it should be accesible at http://example.com/pages/events/2008 without any need for routing.

Adding routes for special pages

The routes allow us to get rid of the /pages/ url and map it directly to the root of the site. So for example http://example.com/pages/about can become http://example.com/about. To achieve that we need to add one line at the end of the routes.php file:

$Route->connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'))

And if you want to have any sub pages in the about folder, then use this line instead:

$Route->connect('/about/*', array('controller' => 'pages', 'action' => 'display', 'about'));

The asterisk means that everything after about/ will be passed on to the controller also.

It is important that we place the line last in the routes.php file because otherwise it would conflict with the core controller mappings. We simply want to catch the requests for which a controller has not been defined.

Changing title and layout of a page

When using a single word as a name for our page, the pages controller automatically capitalises the word and places it as page title. This is convenient but when we dealing with page names separated by dashes it could lead to title like About-us. To solve it we can use this example at the beginning of the page:

$this->pageTitle = 'My page title';

Sometimes there is a need to tell a page to use a different layout instead of the generic one. This can be easily achieved using following line:

< $this layout = 'new_layout';

Replace new_layout with the actual layout name.

Including helpers in your pages

Lets imagine that we have created a helper resize.php which does image resizing in our helpers directory and we want to use it in some of our pages. Before we can do that we need to include the helper definition in the controller, but because we are dealing with the inbuilt pages controller its not residing in the controllers directory. Well there is a way to overwrite the core pages controller by creating pages_controller.php file in the controller directory. We need to copy file found in cake/libs/controller/pages_controller.php into our controllers directory, and then add one line in the PagesController class:

var $helpers = array('Resize');

here Resize is the name of our helper.

After this, the leper is usable in all your static pages. You can use it like so:

$resize->image('filename');

where image is the method in our resize.php helper.

This article was wrtitten by George Tyshchenko last modified on 11 June 2008