是否可以在错误页面上使用AppController? (Cakephp 3.1) [英] Is it possible to use AppController on error pages? (Cakephp 3.1)

查看:266
本文介绍了是否可以在错误页面上使用AppController? (Cakephp 3.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图渲染错误模板(例如error400.ctp),但使用默认布局(网站页眉和页脚),它依赖于在AppController中设置的组件和变量。如何告诉Cake在渲染错误页面时使用AppController?

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?

我已经尝试创建一个扩展AppController的ErrorController,

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

推荐答案

这里是我的工作ErrorController,以防任何人寻找它:

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');
    }

}

我的组件被加载到AppController。当ErrorController扩展AppController,并且一个尝试访问控制器中的无效操作时,它创建了两个AppController实例,在我的情况下,由于我的组件中的一个错误,抛出了类错误的重复声明。此错误导致某种循环,导致错误页面无法呈现。

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.

这篇关于是否可以在错误页面上使用AppController? (Cakephp 3.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆