Cakephp3自定义ExceptionRenderer

时间:2017-05-25 09:23:46

标签: php exception-handling cakephp-3.0

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

app.php

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

并创建了以下脚本:

的src /错误/ 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行]

我无法弄清楚我做错了什么以及如何解决问题。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我发现了问题:

src / Error / AppExceptionRenderer.php

中的

我已取代:

use Cake\Network\Exception\NotFoundException as Exception;

<强>与

use Exception;

它有效!