2017-05-25 43 views
0

我试图建立错误404自定义异常渲染我修改了配置有如下:Cakephp3定制ExceptionRenderer

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):声明应用程序\错误\ AppExceptionRenderer :: _模板()应与蛋糕\错误\ ExceptionRenderer兼容:: _模板(例外$例外,$ method,$ code) [APP/Error/AppExceptionRenderer.php,line 10]

我弄不清楚我在做什么错误以及如何解决问题。有什么建议么?

回答

0

我想通了这个问题:

的src /错误/ AppExceptionRenderer.php

我代替:

use Cake\Network\Exception\NotFoundException as Exception; 

有:

use Exception; 

它的工作原理!