Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple layouts for cakephp?

I am quite confused on how to have my own layout for each page in cakephp. Currently, there is a default.ctp which I have modified to have my main layout and included the $content_for_layout code. So whatever I have entered in the pages\home.ctp gets reflected. But I want to have a login and register layout and also their individual pages. How can I go about achieving this? Should I even edit the default.ctp? Or create another layout for my main page?

Please assist.

like image 954
dwyane Avatar asked Jan 25 '26 06:01

dwyane


2 Answers

You can specify a different layout in your Controller methods, e.g.

function index() {

  $this->layout='my_index_layout'; //app/views/layouts/my_index_layout.ctp

}

function view($id) {

    $this->layout = 'my_view_layout'; //app/views/layouts/my_view/layout.cpt

}
like image 99
RichardAtHome Avatar answered Jan 28 '26 11:01

RichardAtHome


But I want to have a login and register layout and also their individual pages.

The "layout", as understood in Cake, is mostly the header and footer. And it sounds like you are referring to the layout of content. You can do the layout of content in each individual view file.

Should I even edit the default.ctp? Or create another layout for my main page?

Yes, it is there for you to modify. If you want more layouts, you can create more in that folder and specify the layout in the controller (otherwise, it defaults to "default" layout).

like image 32
Anh Pham Avatar answered Jan 28 '26 09:01

Anh Pham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!