Cakephp - how to make error pages have its own layouts?

17,196

Solution 1

Savant from the IRC helped me out and he suggest in using beforeRender(){} in the app_controller

// Before Render
function beforeRender() {
    if($this->name == 'CakeError') {
        //$this->layout = 'error';
    }
}

CakeError is a catchAll for errors :D

Solution 2

In CakePHP 2.2.2 I changed the ExceptionRenderer in core.php with my own, like this:

app/Config/core.php:

Configure::write('Exception', array(
  'handler' => 'ErrorHandler::handleException',
  'renderer' => 'MyExceptionRenderer', // this is ExceptionRenderer by default
  'log' => true
));

app/Lib/Error/MyExceptionRenderer.php:

App::uses('ExceptionRenderer', 'Error');

class MyExceptionRenderer extends ExceptionRenderer {

  protected function _outputMessage($template) {
    $this->controller->layout = 'error';
    parent::_outputMessage($template);
  }

}

Solution 3

Just you need to make layout changes in your error400.ctp file under /app/View/Errors/error400.ctp

Open that file and set layout by

<?php $this->layout=''; //set your layout here ?>

Solution 4

better to create an error.php file in your app folder

class AppError extends ErrorHandler { 
    function error404($params) { 
            $this->controller->layout = 'error'; 
            parent::error404($params); 
    } 
}

so you can avoid the if-testing at EVERY page render that savants' solution introduces

Solution 5

My solution for CakePHP 2.3

Change the ExceptionRenderer in core.php to use your own renderer.

app/Config/core.php:

Configure::write('Exception', array(
  'handler' => 'ErrorHandler::handleException',
  'renderer' => 'MyExceptionRenderer',
  'log' => true
));

app/Lib/Error/MyExceptionRenderer.php:

 App::uses('ExceptionRenderer', 'Error');

 class MyExceptionRenderer extends ExceptionRenderer 
 {
    /**
     * Overrided, to always use a bare controller.
     * 
     * @param Exception $exception The exception to get a controller for.
     * @return Controller
     */
    protected function _getController($exception) {
        if (!$request = Router::getRequest(true)) {
            $request = new CakeRequest();
        }
        $response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
        $controller = new Controller($request, $response);
        $controller->viewPath = 'Errors';
        $controller->layout = 'error';
        return $controller;
    }
 }

The advantage to this approach is that it ensures any exceptions thrown from AppController don't cause an endless loop when rendering the exception. Forces a basic rendering of the exception message every time.

Share:
17,196

Related videos on Youtube

Harsha M V
Author by

Harsha M V

I turn ideas into companies. Specifically, I like to solve big problems that can positively impact millions of people through software. I am currently focusing all of my time on my company, Skreem, where we are disrupting the ways marketers can leverage micro-influencers to tell the Brand’s stories to their audience. People do not buy goods and services. They buy relations, stories, and magic. Introducing technology with the power of human voice to maximize your brand communication. Follow me on Twitter: @harshamv You can contact me at -- harsha [at] skreem [dot] io

Updated on March 09, 2020

Comments

  • Harsha M V
    Harsha M V about 4 years

    I wanna have a different layout for the page not found 404 page. How can i set a different layout for that page?

  • rafasoares
    rafasoares almost 12 years
    This method is no longer valid with CakePHP 2.x. And I have no idea how to do it now... lol
  • Vanja D.
    Vanja D. over 11 years
    I don't confirm in Cake 2.2.1. I debug $this->layout inside beforeRender, I get the expected 'public_layout', but it is still rendered in default.ctp. DEBUG is set to 0.

Related