【发布时间】:2014-08-07 07:03:13
【问题描述】:
我想用 cakephp 为我的项目中的每个控制器创建异常。示例:
class UsersController extends AppController {
public function add(){
if(!$this->User->save($this->request->data)){
throw new UserException('Error save User/add');
}
}
}
class FriendsController extends AppController {
public function add(){
if(!$this->Friend->save($this->request->data)){
throw new FriendException('Error save Friend/add');
}
}
}
我正在尝试这个,但不起作用...永远不要绘制登录类异常用户/朋友
在 app/Config/core.php 中
Configure::write('Exception.handler', 'UserException::handle');
Configure::write('Exception.handler', 'FriendException::handle');
在 app/Config/bootstrap.php 中
App::uses('UserException', 'Lib');
App::uses('FriendException', 'Lib');
在 app/Lib/UserException.php 中
class UserException {
public static function handle($error) {
$this->log($error)
}
}
在 app/Lib/FriendException.php 中
class FriendException {
public static function handle($error) {
$this->log($error)
}
}
有什么想法吗? 问候!
【问题讨论】:
-
你为什么不用
try catch.? -
在每个函数中?我做到了。但是我想在发生错误时发送电子邮件。在日志中始终:致命错误(1):在中找不到类“UserException”