Monday, December 21, 2009

How to create a custom layout type in CakePHP

I have start using cakePHP to create an interface for snom phones supporting the XML minibrowser.

Anyway I wanted to create a separate type for that and keep the xml type for future use. So I decided to create a snom type, with layouts and automatic detection.

1. Create a helper in app/views/helpers/snom.php which extends the core XmlHelper:




2. Create the default layout in app/views/layouts/snom/default.ctp. Mine looks like:



The $snom variable is an instance of the SnomHelper from step 1.

3. Create app/controllers/components/custom_request_handler.php:


4. That's it really. You can start using the new type right away. As said the type will be set automatically according to the user agent but you can force it using extensions; in app/config/routes.php add:


In order to use it in your controller simply load the CustomRequestHandler as:


Then make sure that you have the view set in app/views/viewname/snom/action.ctp

For instance for the previous example you would have to have app/views/example/snom/index.ctp

No comments: