[英]Cakephp3 Custom ExceptionRenderer
I am trying to build a custom exception renderer for error 404. I modified my configuration has follows: 我正在尝试为错误404构建自定义异常渲染器。我修改了配置,如下所示:
app.php app.php
'Error' => [
'errorLevel' => E_ALL,
'exceptionRenderer' => 'App\Error\AppExceptionRenderer', // use my custom renderer
'skipLog' => [],
'log' => true,
'trace' => true,
],
and created the following script: 并创建以下脚本:
src/Error/AppExceptionRenderer.php 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;
}
}
But I get the following error: 但是我收到以下错误:
Strict (2048): Declaration of App\\Error\\AppExceptionRenderer::_template() should be compatible with Cake\\Error\\ExceptionRenderer::_template(Exception $exception, $method, $code) [APP/Error/AppExceptionRenderer.php, line 10] 严格(2048): App \\ Error \\ AppExceptionRenderer :: _ template()的声明应与Cake \\ Error \\ ExceptionRenderer :: _ template(Exception $ exception,$ method,$ code)兼容[APP / Error / AppExceptionRenderer.php,行10]
I can't figure out what I am doing wrong and how to solve the problem. 我无法弄清楚自己在做什么以及如何解决问题。 Any suggestions?
有什么建议么?
I figured out the problem: 我解决了这个问题:
in src/Error/AppExceptionRenderer.php 在src / Error / AppExceptionRenderer.php中
I replaced: 我更换:
use Cake\Network\Exception\NotFoundException as Exception;
with: 与:
use Exception;
And it works! 而且有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.