Free Android CakePHP GMaps Articles by Bali Web Design

July 7, 2009

How to change cakephp layout?

Filed under: php — Tags: — admin @ 3:30 am

The cakephp concept is MVC, Model-View-Controller. This three part will divide the area for database, business logic and design part. MVC give a good system to make a clean code for designer, database analyst and programmer. Design work will not interfere the business logic work, cakephp try to limit this thing.

A layout or html output is construct inside main layout and view for each actions. Main layout is a basic layout for your website. We can have some layout for different output type such as html, xml, rss, or etc. For each type we can have different layout as needed. For example registration page will have different layout with homepage.

By default cakephp use all layouts in folder cake\libs\view\layouts if you are not define your layout. You can override cakephp default layout by create ctp file in folder app/views/layouts. The default layout named as default.ctp. How to change this cakephp layout for certain controller action?

Simple create new ctp file inside app/views/layouts, for example mylayout.ctp. And then inside action function on controller put this code

$this->layout = ‘mylayout’;

or you can set folder path for the layout, for example we have layout file saved on app/views/layouts/xml/mylayout.ctp. you can set the path using this code

$this->layoutPath = ‘xml’;

so the complete code for changing the layout is

$this->layoutPath = ‘xml’;
$this->layout = ‘mylayout’;


3 Comments »

  1. Really usefull.

    Thanks!

    Comment by DjX — March 5, 2010 @ 10:08 am

  2. hi,

    Thanx!

    Comment by Sipatshi — May 26, 2011 @ 6:02 am

  3. i have done
    $this->layoutPath = ‘xml’;
    $this->layout = ‘mylayout’;

    but its not working…showing default.ctp
    i also changed routes.php – Router::connect(‘/’, array(‘controller’ => ‘Home’, ‘action’ => ‘index’));

    Comment by Abdul — March 14, 2012 @ 4:14 am

RSS feed for comments on this post. TrackBack URL

Leave a comment