您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天TP5-20161229|thinkphp5-Controller.php

2016-11-24 08:40 501 查看
<?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 <liu21st@gmail.com>
// +----------------------------------------------------------------------

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 think