我在做教程跟着http://book.cakephp.org/3.0/en/development/errors.html#exception-renderer但它不工作並顯示空白頁面。CakePHP3 - 自定義頁面錯誤404不起作用
在配置/ bootstrap.php中
use App\Error\AppError;
$errorHandler = new AppError();
$errorHandler->register();
在SRC /錯誤/ AppError.php
<?php
namespace App\Error;
use Cake\Error\BaseErrorHandler;
class AppError extends BaseErrorHandler
{
public function _displayError($error, $debug)
{
return 'There has been an error!';
}
public function _displayException($exception)
{
return 'There has been an exception!';
}
public function handleFatalError($code, $description, $file, $line)
{
return 'A fatal error has happened';
}
}
我創建的src /模板/佈局/ my_error.ctp my_error.ctp。並在我的src/Template/Error/error404.ctp中將佈局更改爲my_error.ctp。
$this->layout = 'my_error';
最後,在我的控制器
use Cake\Network\Exception\NotFoundException;
$staff = $this->Staff->find()->where(['Staff.StaffId = '=> $id, 'Staff.PartnerId = ' =>$this->partnerId])->first();
if (empty($staff)) {
throw new NotFoundException(__('Staff not found'));
}