Search code examples
cakephpcakephp-3.0cakephp-3.1

Is it possible to use AppController on error pages? (Cakephp 3.1)


I am trying to render error templates (eg error400.ctp) but with the default layout (site header and footer) which relies on components and variables set in AppController. How do I tell Cake to use AppController when rendering error pages?

I have already tried making an ErrorController which extends AppController, but it breaks for missing actions.


Solution

  • Here's my working ErrorController in case anyone comes looking for it:

    <?php
    namespace App\Controller;
    
    use App\Controller\AppController;
    use Cake\Event\Event;
    
    class ErrorController extends AppController
    {
    
        public function beforeRender(Event $event)
        {
            parent::beforeRender($event);
            $this->viewBuilder()->templatePath('Error');
        }
    
    }
    

    There was a bug in one of my Components being loaded in AppController. When ErrorController extends AppController and one tries to access an invalid action in a controller it creates two instances of AppController and in my case a duplicate declaration of class error was thrown because of a bug in my component. This error caused some kind of loop causing the error page not to render.