繁体   English   中英

Cakephp3自定义ExceptionRenderer

[英]Cakephp3 Custom ExceptionRenderer

我正在尝试为错误404构建自定义异常渲染器。我修改了配置,如下所示:

app.php

'Error' => [
        'errorLevel' => E_ALL,
        'exceptionRenderer' => 'App\Error\AppExceptionRenderer', // use my custom renderer
        'skipLog' => [],
        'log' => true,
        'trace' => true,
    ],

并创建以下脚本:

src / Error / AppExceptionRenderer.php

<?php
namespace App\Error;

use App\Controller\SiteHomepageController;
use Cake\View\View;
use Cake\Event\Event;
use Cake\Error\ExceptionRenderer as ExceptionRenderer;
use Cake\Network\Exception\NotFoundException as Exception;

class AppExceptionRenderer extends ExceptionRenderer {

  protected function _getController(){
    $controller = new SiteHomepageController(null,null,null);
    return $this->controller=$controller;
  }

  public function _template(Exception $exception, $method, $code)
  {
    $template = 'error';
    return $this->template = $template;
  }

  public function render(){

    $exception = $this->error;
    $code = $this->_code($exception);
    $method = $this->_method($exception);
    $template = $this->_template($exception, $method, $code);

    $this->controller->public=true;
    $this->controller->viewBuilder()->layout('site');
    $this->controller->viewBuilder()->templatePath('SiteHomepage');
    $this->controller->set('sez','');
    $this->controller->initialize();
    $this->controller->render($template);

    $event = new Event('');
    $this->controller->afterFilter($event);

    $this->controller->response->statusCode($code);

    return $this->controller->response;

  }

}

但是我收到以下错误:

严格(2048): App \\ Error \\ AppExceptionRenderer :: _ template()的声明应与Cake \\ Error \\ ExceptionRenderer :: _ template(Exception $ exception,$ method,$ code)兼容[APP / Error / AppExceptionRenderer.php,行10]

我无法弄清楚自己在做什么以及如何解决问题。 有什么建议么?

我解决了这个问题:

src / Error / AppExceptionRenderer.php中

我更换:

use Cake\Network\Exception\NotFoundException as Exception;

与:

use Exception;

而且有效!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM