對於我的項目,我創建了一個小型mdl_error
模型。
此模型有一個公共職能,throwError
和一些私人助手,會向用戶顯示閃光通知,並根據需要用當前值和會話數據向我發送電子郵件。該模型是自動加載的,只有在需要時纔會調用。
這裏基本上是什麼樣子:
<?php
class mdl_error extends CI_Model
{
//types: error, alert, good
function throwError($type, $message, $info="", $flash=true, $email=true)
{
if($flash){
$alert = $type."|".$message;
$this->session->set_userdata(array("flash" => $alert));
}
if($email){
$problems = $this->recursivePrintingOfVariables($info);
$sessionData = $this->recursivePrintingOfVariables($this->session->userdata);
$emailMessage = "Name<br/> <br/>Something has happened. <br/> <br/>";
$emailMessage .= "The type was: {$type}<br/>The message was: {$message}<br/> <br/> <br/>";
$emailMessage .= "Here is the local variables at the time:<br/> <br/>{$problems}<br/> <br/> <br/>";
$emailMessage .= "Here is the session data:<br/> <br/>{$sessionData}<br/> <br/> <br/>";
$emailMessage .= "Please solve this problem or we are all dooooooomed.<br/><br/>Love,<br/>Website";
$this->load->library('email');
$this->email->from("my email");
$this->email->to("[email protected]");
$this->email->subject($type.' Message from Website');
$this->email->message($emailMessage);
$this->email->send();
}
}
function recursivePrintingOfVariables($info)
{
$keys = array_keys($info);
$string = "";
foreach($keys as $key){
$string .= $key." => ";
if(is_array($info[$key])){
$string .= "Inner Array<br/>";
$string .= "<div style='margin-left:15px;'>";
$string .= $this->recursivePrintingOfVariables($info[$key]);
$string .= "</div>";
}else{
$string .= $info[$key];
}
$string .= "<br/><br/>";
}
return $string;
}
}
然後,如果在我的代碼中的錯誤發生在一個點,我只要致電:
$this->mdl_error->throwError("error","something happend", get_defined_vars());
難道你找到了答案?我正在嘗試實現類似。 Thx – 2012-07-24 11:23:34