標籤:thinkphp
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <[email protected]>// +----------------------------------------------------------------------namespace think;// 到了 這個控制器了\think\Loader::import(‘controller/Jump‘, TRAIT_PATH, EXT);// 載入控制器 跳轉 trait 函數,// 這個地方呢,老劉啊,不得不說你了,為了使用 trait 而使用,真的不科學use think\Exception;use think\exception\ValidateException;// 使用一系列 異常class Controller{ use \traits\controller\Jump;// 使用 trait 包塊 類 // 視圖類執行個體 protected $view;// 執行個體化視圖 // Request執行個體 protected $request;// 擷取 請求 資訊 // 驗證失敗是否拋出異常 protected $failException = false; // 驗證失敗 異常 // 是否批量驗證 protected $batchValidate = false; // 批量驗證 /** * 前置操作方法列表 * @var array $beforeActionList * @access protected */ protected $beforeActionList = [];// 前置方法列表 /** * 架構函數 * @param Request $request Request對象 * @access public */ public function __construct(Request $request = null)// 建立建構函式 { if (is_null($request)) { $request = Request::instance();// 擷取請求資訊 } $this->view = View::instance(Config::get(‘template‘), Config::get(‘view_replace_str‘));// 執行個體化 view 操作 $this->request = $request;// 賦值 請求 // 控制器初始化 $this->_initialize();// 初始化 控制器 // 前置操作方法 if ($this->beforeActionList) {// 變數前置 foreach ($this->beforeActionList as $method => $options) { is_numeric($method) ? $this->beforeAction($options) : $this->beforeAction($method, $options);// 不同輸入參數的 重設 } } } // 初始化 protected function _initialize()// 空函數 { } /** * 前置操作 * @access protected * @param string $method 前置操作方法名 * @param array $options 調用參數 [‘only‘=>[...]] 或者[‘except‘=>[...]] */ protected function beforeAction($method, $options = []) {// 直接執行函數的前置 操作 if (isset($options[‘only‘])) {// 特殊情況1 if (is_string($options[‘only‘])) { $options[‘only‘] = explode(‘,‘, $options[‘only‘]); } if (!in_array($this->request->action(), $options[‘only‘])) { return; } } elseif (isset($options[‘except‘])) {// 特殊情況2 if (is_string($options[‘except‘])) { $options[‘except‘] = explode(‘,‘, $options[‘except‘]); } if (in_array($this->request->action(), $options[‘except‘])) { return; } } call_user_func([$this, $method]);// 調用 當前自己的函數 } /** * 載入模板輸出 * @access protected * @param string $template 模板檔案名稱 * @param array $vars 模板輸出變數 * @param array $replace 模板替換 * @param array $config 模板參數 * @return mixed */ protected function fetch($template = ‘‘, $vars = [], $replace = [], $config = []) { return $this->view->fetch($template, $vars, $replace, $config); }// 載入模版 輸出 /** * 渲染內容輸出 * @access protected * @param string $content 模板內容 * @param array $vars 模板輸出變數 * @param array $replace 替換內容 * @param array $config 模板參數 * @return mixed */ protected function display($content = ‘‘, $vars = [], $replace = [], $config = []) { return $this->view->display($content, $vars, $replace, $config); }// 顯示輸出 顯示的模版內容 函數有變化了,有意思 /** * 模板變數賦值 * @access protected * @param mixed $name 要顯示的模板變數 * @param mixed $value 變數的值 * @return void */ protected function assign($name, $value = ‘‘) { $this->view->assign($name, $value); }// 普通賦值 /** * 初始化模板引擎 * @access protected * @param array|string $engine 引擎參數 * @return void */ protected function engine($engine) { $this->view->engine($engine); }// 初始化模版引擎 /** * 設定驗證失敗後是否拋出異常 * @access protected * @param bool $fail 是否拋出異常 * @return $this */ protected function validateFailException($fail = true) { $this->failException = $fail; return $this; }// 異常拋出 /** * 驗證資料 * @access protected * @param array $data 資料 * @param string|array $validate 驗證器名或者驗證規則數組 * @param array $message 提示資訊 * @param bool $batch 是否批量驗證 * @param mixed $callback 回調方法(閉包) * @return array|string|true * @throws ValidateException */ protected function validate($data, $validate, $message = [], $batch = false, $callback = null) {// 驗證資料 又跑到這裡了 if (is_array($validate)) {// 如果是 數組 $v = Loader::validate(); $v->rule($validate); } else { if (strpos($validate, ‘.‘)) { // 支援情境 list($validate, $scene) = explode(‘.‘, $validate);// 情境驗證 } $v = Loader::validate($validate); if (!empty($scene)) { $v->scene($scene); } } // 是否批量驗證 if ($batch || $this->batchValidate) {// 批量驗證 $v->batch(true); } if (is_array($message)) {// 訊息處理 $v->message($message); } if ($callback && is_callable($callback)) {// 資訊回調 call_user_func_array($callback, [$v, &$data]); } if (!$v->check($data)) { if ($this->failException) { throw new ValidateException($v->getError()); } else { return $v->getError(); } } else { return true; } }}// 這個版本的控制器簡化了,但是把這個 驗證弄過來 就不應該了
本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1876008
[李景山php]每天TP5-20161229|thinkphp5-Controller.php