Assigning Layout in cakephp

ⅰ亾dé卋堺 提交于 2019-12-04 13:04:38

use it:

inside your action

$this->layout = 'mylayout';

you have to create that layout in view/layout/mylayout.ctp

or add this function to controller to set layout for each action of controller

  function beforeFilter() {
    parent::beforeFilter();
        $this->layout = 'mylayout';
  }

The best way to do some thing like this

var $layout = 'my_account';

This will apply to your entire controller. After adding this code it will look something like this.

class MyAccountsController extends AppController {
    var $name = 'MyAccounts';
    var $components = array('Email');
    var $layout = 'my_account';

If you do not want to use some of the action you can explicitly define in your action like this

function my_action(){
    $this->layout = 'another_layout';
}

Now this my_action will take another layout and rest of the action will take my_account layout

For CakePHP 3.0 Red Velvet

Layout file location:

New param:

$this->viewBuilder()->layout("loginUI");

*"loginUI" is file name layout

Yes we can. You just need to create your layout file under Template>>Layout>>yourlayout.ctp Then load this layout along with the Controller by:

class MyController extends AppController {
    public function initialize(){
        parent :: initialize();
        $this->layout = "yourlayout";

      }
}

this will automatically implement this layout as default for the Controller. you might want to use $this->viewBuilder->layout("yourlayout"); inside the initialize() but it leads to error "Call to member function layout() on boolean". So instead you can use the former.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!