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

ぐ巨炮叔叔 提交于 2019-12-10 18:33:28

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/33103872/is-it-possible-to-use-appcontroller-on-error-pages-cakephp-3-1

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