Beispiel #1
0
 /**
  * 视图渲染函数,
  * @param $data,这是要传到html文件中使用的参数
  * @return null
  * */
 public static function render($tpl, $data = null)
 {
     //增加这一个循环将传过来的$data数组中的变量array(index => value)通过 $index就可以访问到,兼容之前的$data['index']也能访问到
     if (!empty($data)) {
         foreach ($data as $key => $value) {
             ${$key} = $value;
         }
     }
     $tplPath = VIEW_FRONT_PATH . $tpl . '.php';
     if (!file_exists($tplPath)) {
         EException::throwException('The template file: ' . $tpl . '.php in ' . $tplPath . ' does not exist!');
     }
     //引入html文件
     include $tplPath;
 }
Beispiel #2
0
 /**
  * 路由管理器函数
  * index.php?r=controllerName/funcName 这个url会去到controllerName这个类执行funcName这个函数
  * */
 public static function urlManager()
 {
     //根据是前台还是后台对到对应的页面,因为前台后设置的默认控制器不一样
     //在new Abcontroller 或者new Abmodel的时候会触发autoload函数,而autoload函数对前后台做了区分,
     //所以在前后台不同的目录下时会创建分别对应前后台不同的类对象
     if (IS_ADMIN) {
         //如果没有指定控制器则默认去到 site/index
         $service = empty($_GET['r']) ? 'default/index' : $_GET['r'];
     } else {
         $service = empty($_GET['r']) ? 'site/index' : $_GET['r'];
     }
     $arr = explode('/', $service);
     //ucfirst() 首字母大写函数
     $className = ucfirst($arr[0]) . 'controller';
     //如果没有指定函数则去到 index函数
     if (isset($arr[1])) {
         $methodName = trim($arr[1]) ? $arr[1] : 'index';
     } else {
         $methodName = 'index';
     }
     //正则表达式验证url是否合法 这里的controllerName,funcName只能是26个字母
     if (!preg_match('/([a-zA-Z])\\/([a-zA-Z])/', $className . '/' . $methodName)) {
         EException::throwException('Illegal url!');
     }
     //验证url请求是否有对应的类和函数
     if (!class_exists($className)) {
         if (defined('CMS_DEGUB') && CMS_DEGUB) {
             //开启调试模式则报出错误信息
             EException::throwException('The ' . $className . ' class does not exit!');
         } else {
             //不开启调试则自动处理成默认的controller和method
             $className = IS_ADMIN ? 'Defaultcontroller' : 'Sitecontroller';
         }
     }
     if (!method_exists($className, $methodName)) {
         if (defined('CMS_DEGUB') && CMS_DEGUB) {
             //开启调试模式则报出错误信息
             EException::throwException('The method ' . $methodName . ' does not exit in class ' . $className . '!');
         } else {
             //不开启调试则自动处理成默认的controller和method
             $methodName = 'index';
         }
     }
     //根据类名$className创建一个类对象,这点php和其它语言有点不同
     $classObject = new $className();
     //调用call_user_func函数去执行$classObject类里面的$methodName函数
     call_user_func(array($classObject, $methodName));
 }
 public static function catchException(EException $e)
 {
     $logger = new Logger();
     $logger->error($e->getMessage(), ['code' => $e->getCode(), 'info' => $e->getInfo(), 'trace' => array_shift($e->getTrace())]);
     $e->view->message = $e->getMessage();
     $e->view->display($e->path . 'exception.php');
 }
Beispiel #4
0
 /**
  *  pdo 执行多条insert update delete语句
  * @param string $sqlParamsArr, 传递过来的sql和参数数组
  * array (
  *          array('sql' => 'sql string', 'params' => array('placeholder' => 'paramValue'))  或者 array('sql string', array('placeholder' => 'paramValue')) ,
  *          array(),
  *          ...
  *        )
  * @return boolean ,执行成功返回真,执行失败返回flase
  * */
 public function multipleExecute($sqlParamsArr)
 {
     if (!(is_array($sqlParamsArr) && count($sqlParamsArr))) {
         EException::throwException('the second arguemnts of fetchOne() function is not correct');
     }
     $pdo = DB::getPDO();
     $pdo->beginTransaction();
     foreach ($sqlParamsArr as $sqlParams) {
         if (!(is_array($sqlParams) && count($sqlParams))) {
             EException::throwException('the second arguemnts of fetchOne() function is not correct');
         }
         $stmt = $pdo->prepare($sqlParams['sql']);
         if (is_array($sqlParams['params']) && count($sqlParams)) {
             foreach ($sqlParams['params'] as $key => $value) {
                 $stmt->bindValue($key, $value);
                 //bindValue和bindParam的区别!bindValue()直接将变量的值绑定到语句中, 而bindParam将变量作为引用进行绑定,并只在 PDOStatement::execute() 被调用的时候才取其值。
                 //多个参数循环后赋给第一个参数的值变成了后面的绑定变量的值从而导致类型出错导致sql执行出错
                 //同一组参数内多个占位符进行赋值的时候用bindValue(),针对不同组但组的类型相同时则用bindParam
             }
         }
         $stmt->execute();
         //$stmt->debugDumpParams();
         if (!$stmt->rowCount()) {
             $pdo->rollBack();
             //某一条语句执行失败,回滚
             return false;
             //并返回false
         }
         $stmt->closeCursor();
         //关闭游标
     }
     $pdo->commit();
     return true;
 }
Beispiel #5
0
 /**
  *  pdo 执行多条insert update delete语句
  * @param string $sqlParamsArr, 传递过来的sql和参数数组
  * array (
  *          array('sql' => 'sql string', ['params' => array('placeholder' => 'paramValue')]) ,
  *          array(),
  *          ...
  *        )
  * @return boolean ,执行成功返回真,执行失败返回flase
  * */
 public function multipleExecute($sqlParamsArr, $rollBack = true, &$errorRowArr = array(), &$affectedRows = 0)
 {
     if (!(is_array($sqlParamsArr) && count($sqlParamsArr))) {
         EException::throwException('the first arguemnts of multipleExecute() function is not correct');
     }
     $pdo = DB::getPDO();
     $pdo->beginTransaction();
     foreach ($sqlParamsArr as $sqlParams) {
         if (!(is_array($sqlParams) && count($sqlParams))) {
             EException::throwException('the second arguemnts of multipleExecute() function is not correct');
         }
         $stmt = $pdo->prepare($sqlParams['sql']);
         if (isset($sqlParams['params']) && is_array($sqlParams['params']) && count($sqlParams)) {
             foreach ($sqlParams['params'] as $key => $value) {
                 $stmt->bindValue($key, $value);
                 //bindValue和bindParam的区别!bindValue()直接将变量的值绑定到语句中, 而bindParam将变量作为引用进行绑定,
                 //并只在 PDOStatement::execute() 被调用的时候才取其值。
                 //多个参数循环后赋给第一个参数的值变成了后面的绑定变量的值从而导致类型出错导致sql执行出错
                 //同一组参数内多个占位符进行赋值的时候用bindValue(),针对不同组但组的类型相同时则用bindParam
             }
         }
         $stmt->execute();
         //$stmt->debugDumpParams();
         if (!$stmt->rowCount() && $sqlParams['userFlag'] == false) {
             //开启多条语句出错回滚机制
             if (!$rollBack) {
                 $errorRowArr[] = array('sql' => $sqlParams['sql'], 'sheetName' => $sqlParams['sheetName'], 'row' => $sqlParams['row']);
             } else {
                 $pdo->rollBack();
                 //某一条语句执行失败,回滚
                 return false;
                 //并返回false
             }
         } else {
             if ($sqlParams['userFlag'] == false) {
                 $affectedRows = $affectedRows + 1;
             }
         }
         $stmt->closeCursor();
         //关闭游标
     }
     $pdo->commit();
     return true;
 }