Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing only the home page layout in cakephp

If my whole site is using the default.ctp layout specified in apps/view/layouts/default.ctp, how do I change only the home page layout to use homepage.ctp and leave the rest of the site using default.ctp?

like image 410
Bradley Avatar asked Dec 21 '09 09:12

Bradley


People also ask

How do I change the default layout in CakePHP?

Try changing the $layout property in the controller itself. Show activity on this post. In CakePHP 3.6. 2 you can not use $this->layout because is deprecated.

What is element in CakePHP?

Certain parts of the web pages are repeated on multiple web pages, but at different locations. CakePHP can help us reuse these repeated parts. These reusable parts are called Elements - help box, extra menu, etc. An element is basically a mini-view. We can also pass variables in elements.


2 Answers

Copy the /cake/libs/controller/pages_controller.php into your /app/controller/ dir and do either of the following:

  • Add a line towards the end of display() to switch the layout if 'home' is requested:
    if ($page == 'home') $this->layout = 'homepage';
  • Create a home() method (or named however you like) in which you set $this->layout and re-route the / route in /app/config/routes.php to use this new method.

Edit:
In summary, you need some custom method in which you'll set $this->layout = 'homepage', that's all. You can do this in any of your controllers at any point, reusing the PagesController is just the most convenient and conventional way to do it in Cake.

like image 77
deceze Avatar answered Nov 13 '22 05:11

deceze


The above answer is now out of date, but gives the right approach.

In modern versions of CakePHP, the controller he asks you to make is already present and is:

/app/Controller/PagesController.php

I had a template called "loggedoff", and added this as follows, just before the $this->render() command (approx line 73).

$this->layout = 'loggedoff';
like image 30
elb98rm Avatar answered Nov 13 '22 04:11

elb98rm