2017-05-25 48 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; 

它的工作原理!