6

Individually I am able to change cakephp default layout by using controller.For example I have used

public function login() {
    $this->layout="make";  //here I have changed layout for single action

        if ($this->request->is('post')) {
            //some code...
        } 
   }

Here I have changed layout!! But the problem is this layout is not default.I want to apply this layout for all controller.How can I do this?

1
  • do it in your AppController
    – arilia
    Mar 24, 2014 at 10:38

4 Answers 4

13

In your AppController

public function beforeRender() {
    parent::beforeRender();

    $this->layout = 'custom';
}
1
  • 1
    This answer is perfect, but in version 3.4 (as i am using) , this is deprecated, you have to use $this->viewBuilder()->setLayout("custom") method to set your layout. Jun 15, 2017 at 13:17
5

As of CakePHP 3.1, view layouts have changed:

// In a controller, instead of
$this->layout = 'advanced';

// You should use
$this->viewBuilder()->layout('advanced');
2

This answer may no longer apply in CakePHP 3.0.5. It requires an event object as an argument in the beforeRender() function.

Try changing the $layout property in the controller itself.

public $layout = 'non-default'

will change the layout used by all views defined by the controller to src/Template/Layout/non-default.ctp

1

In CakePHP 3.6.2 you can not use $this->layout because is deprecated. try following code.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.