Skip to main content
PHP Blog

Back to all posts

How to Change the Layout In Cakephp?

Published on
10 min read
How to Change the Layout In Cakephp? image

Best CakePHP Layout Customization Tools to Buy in November 2025

1 Cake Decorating Tools Supplies Kit - 82Pcs Baking Accessories with Turntable Stand Leveler 12 Piping Tips 52 Bags Icing Comb Scrapers Spatulas Cupcake Cookie Frosting Fondant Bakery Set for Beginners

Cake Decorating Tools Supplies Kit - 82Pcs Baking Accessories with Turntable Stand Leveler 12 Piping Tips 52 Bags Icing Comb Scrapers Spatulas Cupcake Cookie Frosting Fondant Bakery Set for Beginners

  • COMPLETE KIT: ALL TOOLS NEEDED FOR STUNNING CAKES IN ONE SET!

  • PRO-LEVEL DECORATING: 360° TURNTABLE FOR FLAWLESS CAKE DESIGNS!

  • VERSATILE PIPING: 12 TIPS & BOTH DISPOSABLE AND REUSABLE BAGS INCLUDED!

BUY & SAVE
$31.99
Cake Decorating Tools Supplies Kit - 82Pcs Baking Accessories with Turntable Stand Leveler 12 Piping Tips 52 Bags Icing Comb Scrapers Spatulas Cupcake Cookie Frosting Fondant Bakery Set for Beginners
2 Cake Decorating Kit, 138pcs Cake Decorating Supply with Aluminium Cake Turntable, 64 Numbered Icing Piping Tips, 2 Spatulas, 3 Icing Comb Scraper and Leveller, 32 Piping Bags,cake decorating tool

Cake Decorating Kit, 138pcs Cake Decorating Supply with Aluminium Cake Turntable, 64 Numbered Icing Piping Tips, 2 Spatulas, 3 Icing Comb Scraper and Leveller, 32 Piping Bags,cake decorating tool

  • ALL AGES FUN: PERFECT FOR BEGINNERS AND PROS – EVERYONE CAN DECORATE!
  • USER-FRIENDLY KIT: NUMBERED TIPS & PATTERN CHARTS TAKE GUESSWORK OUT!
  • VERSATILE TOOLS: COMPLETE SET FOR STUNNING DESIGNS, NO MESS, EASY CLEAN-UP!
BUY & SAVE
$34.66
Cake Decorating Kit, 138pcs Cake Decorating Supply with Aluminium Cake Turntable, 64 Numbered Icing Piping Tips, 2 Spatulas, 3 Icing Comb Scraper and Leveller, 32 Piping Bags,cake decorating tool
3 MDLJG Cake Cream Spatula 5 Pieces, Stainless Steel Cake Apatula with Frosting Icing decorating Knife Art Painting Baking Pastry Tool Palette Knife for Cake Fondant Chocolate (5 Pcs)

MDLJG Cake Cream Spatula 5 Pieces, Stainless Steel Cake Apatula with Frosting Icing decorating Knife Art Painting Baking Pastry Tool Palette Knife for Cake Fondant Chocolate (5 Pcs)

  • VERSATILE SIZES: MEET ALL YOUR CAKE DECORATING NEEDS EFFORTLESSLY.
  • DURABLE MATERIAL: HIGH-QUALITY STAINLESS STEEL ENSURES LONG-LASTING USE.
  • ERGONOMIC DESIGN: COMFORTABLE GRIP PREVENTS FATIGUE DURING EXTENDED USE.
BUY & SAVE
$6.92 $8.99
Save 23%
MDLJG Cake Cream Spatula 5 Pieces, Stainless Steel Cake Apatula with Frosting Icing decorating Knife Art Painting Baking Pastry Tool Palette Knife for Cake Fondant Chocolate (5 Pcs)
4 Kootek 96PCs Cake Decorating Kits, Baking Supplies Tools with Cake Stand Turntable, Piping Bags and Tips, Cake Leveler, Frosting Icing Spatulas & More Accessories for Cake Cupcake Decorations

Kootek 96PCs Cake Decorating Kits, Baking Supplies Tools with Cake Stand Turntable, Piping Bags and Tips, Cake Leveler, Frosting Icing Spatulas & More Accessories for Cake Cupcake Decorations

  • COMPLETE KIT: EVERYTHING YOU NEED FOR FLAWLESS CAKE DECORATING!
  • USER-FRIENDLY: EASY-TO-FOLLOW EBOOK AND PATTERN CHART INCLUDED.
  • PROFESSIONAL TOOLS: HIGH-QUALITY SPATULAS AND VERSATILE PIPING TIPS!
BUY & SAVE
$19.99 $22.99
Save 13%
Kootek 96PCs Cake Decorating Kits, Baking Supplies Tools with Cake Stand Turntable, Piping Bags and Tips, Cake Leveler, Frosting Icing Spatulas & More Accessories for Cake Cupcake Decorations
5 4 PCS Clear Acrylic Cake Scraper Smoother, Cake Smoother for Icing, Cake Scraper, Cake Stripes Edge Contour Comb for Cream Decorating Kitchen Baking Tool, Mousse Pastry Cutter, DIY

4 PCS Clear Acrylic Cake Scraper Smoother, Cake Smoother for Icing, Cake Scraper, Cake Stripes Edge Contour Comb for Cream Decorating Kitchen Baking Tool, Mousse Pastry Cutter, DIY

  • 7 SHAPES & DOUBLE-SIDED: FLEXIBILITY FOR UNIQUE CAKE TEXTURES!
  • DURABLE ACRYLIC MATERIAL: STURDY, FLAWLESS FINISH FOR EVERY CAKE!
  • HANDLELESS DESIGN: ULTIMATE CONTROL FOR PERFECT CAKE DECORATING!
BUY & SAVE
$6.99 $7.59
Save 8%
4 PCS Clear Acrylic Cake Scraper Smoother, Cake Smoother for Icing, Cake Scraper, Cake Stripes Edge Contour Comb for Cream Decorating Kitchen Baking Tool, Mousse Pastry Cutter, DIY
6 Kootek Aluminium Cake Turntable, 12 Inch Rotating Cake Stand, 35 pcs Cake Decorating Kit Supplies with 24 Numbered Icing Piping Tips, 2 Frosting Spatula, Pastry Bags and Other Baking Tools

Kootek Aluminium Cake Turntable, 12 Inch Rotating Cake Stand, 35 pcs Cake Decorating Kit Supplies with 24 Numbered Icing Piping Tips, 2 Frosting Spatula, Pastry Bags and Other Baking Tools

  • ALL-IN-ONE KIT: BAKE, DECORATE, AND CREATE WITH EASE AND PRECISION!
  • HEAVY-DUTY TURNTABLE: 360° ROTATION FOR FLAWLESS CAKE DECORATING EVERY TIME!
  • STEP-BY-STEP GUIDE: E-BOOK TIPS ELEVATE YOUR BAKING SKILLS EFFORTLESSLY!
BUY & SAVE
$45.99
Kootek Aluminium Cake Turntable, 12 Inch Rotating Cake Stand, 35 pcs Cake Decorating Kit Supplies with 24 Numbered Icing Piping Tips, 2 Frosting Spatula, Pastry Bags and Other Baking Tools
7 4 Pcs Cake Arch Guide Tool, Cake Arc Ruler, Cake Arch Tool Convenient for Controlling the Size and Position of the Arc, Suitable for Beginners and Bakers.

4 Pcs Cake Arch Guide Tool, Cake Arc Ruler, Cake Arch Tool Convenient for Controlling the Size and Position of the Arc, Suitable for Beginners and Bakers.

  • DURABLE & REUSABLE: HIGH-QUALITY MATERIAL FOR LASTING PERFORMANCE.
  • PRECISION MARKING: 14 WIDTHS FOR IMPECCABLE CAKE DECORATIONS.
  • INSPIRE CREATIVITY: IDEAL FOR BAKERS OF ALL SKILL LEVELS.
BUY & SAVE
$7.99
4 Pcs Cake Arch Guide Tool, Cake Arc Ruler, Cake Arch Tool Convenient for Controlling the Size and Position of the Arc, Suitable for Beginners and Bakers.
8 RFAQK 35PCs Cake Turntable and Leveler-Rotating Cake Stand with Non Slip pad-7 Icing Tips and 20 Bags- Straight & Offset Spatula-3 Scraper Set -EBook-Cake Decorating Supplies Kit -Baking Tools

RFAQK 35PCs Cake Turntable and Leveler-Rotating Cake Stand with Non Slip pad-7 Icing Tips and 20 Bags- Straight & Offset Spatula-3 Scraper Set -EBook-Cake Decorating Supplies Kit -Baking Tools

  • COMPLETE TOOLKIT FOR PROFESSIONAL CAKE DECORATING AT HOME!
  • SMOOTH, STABLE TURNTABLE ENSURES FLAWLESS ICING EVERY TIME.
  • BEGINNER-FRIENDLY EBOOK GUIDES YOU THROUGH CAKE DECORATING!
BUY & SAVE
$15.79 $16.79
Save 6%
RFAQK 35PCs Cake Turntable and Leveler-Rotating Cake Stand with Non Slip pad-7 Icing Tips and 20 Bags- Straight & Offset Spatula-3 Scraper Set -EBook-Cake Decorating Supplies Kit -Baking Tools
+
ONE MORE?

To change the layout in CakePHP, you need to follow these steps:

  1. Locate the layout file: In CakePHP, layouts are stored in the src/Template/Layout directory. Each controller typically has its own layout file.
  2. Customize the layout file: Open the layout file corresponding to the controller you want to change. By default, the layout file is named default.ctp. You can use any text editor to modify this file.
  3. Modify the HTML structure: Within the layout file, you can change the HTML structure to meet your requirements. This includes adding or removing HTML elements, modifying CSS classes, or rearranging existing elements.
  4. Use CakePHP helpers: CakePHP provides various helpers that assist in generating HTML content. You can utilize these helpers within the layout file to dynamically generate content or include components common to multiple views, such as menus or headers.
  5. Save and apply changes: Once you have finished modifying the layout file, save your changes and refresh the corresponding page in your application to see the updated layout.

Remember that changing the layout affects all the views associated with the controller. If you want to change the layout for a specific action only, you can create a separate layout file using the desired naming convention. For example, action_name.ctp. Then, within the corresponding action's controller function, specify the layout using $this->viewBuilder()->setLayout('action_name');.

These steps provide a basic overview of how to change the layout in CakePHP. Further customization can be done based on specific requirements, but this should give you a good starting point.

How do I disable layout caching in CakePHP?

To disable layout caching in CakePHP, you can follow these steps:

  1. Open the AppController.php file in your CakePHP application.
  2. Add the following line of code at the top of the file to import the necessary class: use Cake\Event\EventInterface;
  3. Inside the beforeRender() method in AppController.php, add the following code: public function beforeRender(EventInterface $event) { parent::beforeRender($event); // Disable layout caching $this->response = $this->response->withDisabledCache(); }
  4. Save the AppController.php file.

By calling the $this->response->withDisabledCache() method within the beforeRender() method, you are instructing CakePHP to disable the layout caching for the current request.

How do I pass data from the controller to the layout in CakePHP?

In CakePHP, you can pass data from the controller to the layout by using the set() method. Here's how you can do it:

  1. In your controller action, use the set() method to pass the data to the layout. For example:

// Controller action code public function example() { $data = 'Hello World'; // Data to be passed to the layout $this->set('data', $data); }

  1. In your layout file (usually located in src/Template/Layout/default.ctp), you can access the data using the variable name that you set in the controller. For example:
<!-- Other layout content -->

<?php echo $this->fetch('content'); ?> <!-- Render the view content -->

<!-- Other layout content -->

In the above example, the $data variable is passed from the example() action of the controller to the layout file. It can then be accessed and displayed in the layout using <?php echo $data; ?>.

How do I swap layouts in a CakePHP project?

In a CakePHP project, you can swap layouts by performing the following steps:

  1. Generate a new layout file: Create a new layout file in the src/Template/Layout directory with the extension .ctp (e.g., new_layout.ctp).
  2. Customize the new layout file: Open the new layout file and modify it according to your requirements and design. Include the necessary HTML, CSS, and CakePHP elements.
  3. Apply the new layout to a specific view: Open the controller associated with the view where you want to apply the new layout. In the action method, set the layout property to the name of the layout file you created. For example, to apply the new_layout.ctp file to the index action of a PostsController, you can add the following code:

public function index() { $this->viewBuilder()->setLayout('new_layout'); }

  1. Verify the layout swap: Load the view associated with the action you modified, and the new layout should be applied.
  2. Apply the new layout to multiple views: If you want to apply the new layout to multiple views, you can modify the initialize method of the controller. For example:

public function initialize(): void { parent::initialize(); $this->viewBuilder()->setLayout('new_layout'); }

By following these steps, you can successfully swap layouts in a CakePHP project. Remember to customize the layout file according to your desired design and logic.

What is the significance of the "default.ctp" file in CakePHP layouts?

The "default.ctp" file in CakePHP layouts is the default template file used by the application to render the HTML layout for each page. It acts as a wrapper around the content of individual views and helps to maintain a consistent look and feel across the entire application.

The "default.ctp" file typically contains the common elements of a webpage, such as the header, footer, navigation menu, and any other elements that are shared across multiple views. By defining these elements in the layout file, developers can avoid duplicating code in each of the view files and make it easier to update the layout throughout the application.

Additionally, the layout file can also include placeholders or "blocks" that allow content from individual views to be inserted dynamically. These blocks can be defined in the layout file and then filled with content from the respective view file. This allows for flexibility in customizing the layout for different views while still maintaining a consistent structure.

Overall, the "default.ctp" file plays a significant role in CakePHP layouts by providing a reusable template for rendering HTML pages, creating a consistent user experience, and allowing for customization of individual views within the layout.

Can I use a different layout for administrative sections of my CakePHP application?

Yes, you can use a different layout for the administrative sections of your CakePHP application.

In CakePHP, you can specify the layout to be used for a specific controller or action by setting the $layout variable in the respective controller or action.

For example, if you have an AdminsController handling the administrative section of your application, you can define a different layout by adding the following code in the AdminsController file:

class AdminsController extends AppController { /** * The layout used for the administrative section * @var string */ public $layout = 'admin';

// Rest of the controller code...

}

Here, the $layout variable is set to 'admin', which should correspond to a layout file named admin.ctp located in the src/Template/Layout directory.

You can create this admin.ctp layout file in the src/Template/Layout directory and design it as per your requirements specifically for the administrative section of your application.

Once you have created the layout file, all the views rendered by the AdminsController and its actions will use this admin layout.

You can customize the layout for any other controllers or actions in a similar way by setting the $layout variable in the respective class or action.

Note: $layout setting at the controller level applies to all actions within that controller unless overridden explicitly in an action itself.

How do I customize the layout for the login page in CakePHP?

To customize the layout for the login page in CakePHP, you can follow these steps:

  1. Create a new layout file: First, create a new layout file specifically for the login page. This layout file will be used only for the login action. You can create this file in the src/Template/Layout/ directory. For example, you can create a file named login.ctp.
  2. Customize the layout: Open the newly created login.ctp file and modify it according to your needs. You can add HTML, CSS, and JavaScript code to design the layout as desired. Make sure to include the necessary CakePHP elements and variables, such as fetch('content') ?>, which will render the content specific to the login page.
  3. Specify the layout for the login action: Now, go to the controller responsible for handling the login functionality (e.g., UsersController) and locate the login action. Add the following line of code before the render() method call:

$this->viewBuilder()->setLayout('login');

Replace 'login' with the name of the layout file you created in step 1 (login.ctp). This tells CakePHP to use the specified layout for the login action.

  1. Optionally, apply the layout to other actions: If you want to use the same layout for other actions associated with the login page (e.g., register, forgotPassword, etc.), you can repeat step 3 for those actions as well.

By following these steps, you can customize the layout for the login page in CakePHP according to your requirements.

How do I create a new layout in CakePHP?

To create a new layout in CakePHP, you need to follow these steps:

  1. Create a new layout file: Open your project's "src/Template/Layout" directory and create a new file with a .ctp extension, e.g., mylayout.ctp.
  2. Customize the layout file: Open the newly created layout file and customize it according to your requirements. A layout file typically contains HTML markup, along with placeholders that will be filled with content from individual views. Here's a sample layout file structure:

In the above example, <?= $this->fetch('title') ?> and <?= $this->fetch('content') ?> are placeholders that will be filled with the corresponding values from the views.

  1. Set the new layout for specific views: Open the view file for which you want to use the new layout (e.g., src/Template/Posts/index.ctp), and define the layout you want to use by including the following line at the top of the file:

$this->layout = 'mylayout';

Replace 'mylayout' with the name of your newly created layout file (without the .ctp extension).

That's it! The views using the specified layout will now render using the newly created layout file.

What is the default layout in CakePHP?

The default layout in CakePHP is called "default.ctp". It is located in the "src/Template/Layout" directory of a CakePHP project. This layout file defines the overall structure and design of the web pages in the application, including the header, footer, and any shared elements.

Can I override the default layout in CakePHP for a specific controller?

Yes, you can override the default layout in CakePHP for a specific controller.

To do this, you need to set the $layout variable in the controller's action or beforeFilter method. For example, if you have a PostsController and you want to use a custom layout called 'custom_layout.ctp', you would add the following code inside the beforeFilter method or in a specific action:

$this->viewBuilder()->setLayout('custom_layout');

This will set the layout 'custom_layout.ctp' specifically for the actions of the PostsController. You can replace 'custom_layout' with the name of your desired layout file.

Note: This method only changes the layout for the specific controller, it won't affect the default layout used by other controllers.