码迷,mamicode.com
首页 > Web开发 > 详细

[李景山php]每天TP5-20161221|thinkphp5-jump.php

时间:2016-11-11 15:36:48      阅读:588      评论:0      收藏:0      [点我收藏+]

标签:thinkphp

<?php

/**
 * 用法:
 * load_trait(‘controller/Jump‘);
 * class index
 * {
 *     use \traits\controller\Jump;
 *     public function index(){
 *         $this->error();
 *         $this->redirect();
 *     }
 * }
 */
namespace traits\controller;

use think\Config;
use think\exception\HttpResponseException;
use think\Request;
use think\Response;
use think\response\Redirect;
use think\Url;
use think\View as ViewTemplate;

trait Jump
{// 跳转 包块 ,一个包块
    /**
     * 操作成功跳转的快捷方法
     * @access protected
     * @param mixed     $msg 提示信息
     * @param string    $url 跳转的URL地址
     * @param mixed     $data 返回的数据
     * @param integer   $wait 跳转等待时间
     * @param array     $header 发送的Header信息
     * @return void
     */
    protected function success($msg = ‘‘, $url = null, $data = ‘‘, $wait = 3, array $header = [])
    {// 成功跳转
        $code = 1;// 标志位
        if (is_numeric($msg)) {// 如果发送的信息是数字,应该会有一些特殊的函数,根据输入参数的不同的 类型,选择不同的执行方案,貌似越来越流行了
            $code = $msg;// 把数字 赋值给编码
            $msg  = ‘‘;// 清空消息
        }
        if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {// 如果需要跳转的地址留空,并且拥有 来路信息
            $url = $_SERVER["HTTP_REFERER"];// 跳转的地址 就是返回地址
        } elseif (‘‘ !== $url) {// 如果 指定的跳转地址
            $url = preg_match(‘/^(https?:|\/)/‘, $url) ? $url : Url::build($url);// 如果指定了 http: 则不在解析,否则,重建标准的http信息
        }
        $result = [// 返回结果集
            ‘code‘ => $code,// 编码
            ‘msg‘  => $msg,//信息
            ‘data‘ => $data,// 返回的数据
            ‘url‘  => $url,// url 地址
            ‘wait‘ => $wait,// 等待时间
        ];

        $type = $this->getResponseType();// 获取返回类型 就是 ajax 返回,还是 普通的返回
        if (‘html‘ == strtolower($type)) {//如果是普通的 html 方式返回
            $result = ViewTemplate::instance(Config::get(‘template‘), Config::get(‘view_replace_str‘))
                ->fetch(Config::get(‘dispatch_success_tmpl‘), $result);// 传入返回结果参数,生成对应的 Html 页面
        }
        $response = Response::create($result, $type)->header($header);// 返回信息 根据 这些 结果字符串,类型,头信息返回
        throw new HttpResponseException($response);// 抛出异常 【返回处理信息】
    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param mixed     $msg 提示信息
     * @param string    $url 跳转的URL地址
     * @param mixed     $data 返回的数据
     * @param integer   $wait 跳转等待时间
     * @param array     $header 发送的Header信息
     * @return void
     */
    protected function error($msg = ‘‘, $url = null, $data = ‘‘, $wait = 3, array $header = [])
    {// 错误处理
        $code = 0;
        if (is_numeric($msg)) {
            $code = $msg;
            $msg  = ‘‘;
        }// 如果是数字
        if (is_null($url)) {// 如果为空 用js 返回上一步
            $url = ‘javascript:history.back(-1);‘;
        } elseif (‘‘ !== $url) {
            $url = preg_match(‘/^(https?:|\/)/‘, $url) ? $url : Url::build($url);
        }// 处理 url 类型
        $result = [
            ‘code‘ => $code,
            ‘msg‘  => $msg,
            ‘data‘ => $data,
            ‘url‘  => $url,
            ‘wait‘ => $wait,
        ];// 输入的结果参数 $result_inupt

        $type = $this->getResponseType();// 同样的返回处理方案
        if (‘html‘ == strtolower($type)) {
            $result = ViewTemplate::instance(Config::get(‘template‘), Config::get(‘view_replace_str‘))
                ->fetch(Config::get(‘dispatch_error_tmpl‘), $result);
        }
        $response = Response::create($result, $type)->header($header);
        throw new HttpResponseException($response);
    }

    /**
     * 返回封装后的API数据到客户端
     * @access protected
     * @param mixed     $data 要返回的数据
     * @param integer   $code 返回的code
     * @param mixed     $msg 提示信息
     * @param string    $type 返回数据格式
     * @param array     $header 发送的Header信息
     * @return void
     */
    protected function result($data, $code = 0, $msg = ‘‘, $type = ‘‘, array $header = [])
    {// 对array 类型的数据 进行 返回结果封装
        $result = [
            ‘code‘ => $code,
            ‘msg‘  => $msg,
            ‘time‘ => $_SERVER[‘REQUEST_TIME‘],
            ‘data‘ => $data,
        ];
        $type     = $type ?: $this->getResponseType();
        $response = Response::create($result, $type)->header($header);// 创建返回 封装
        throw new HttpResponseException($response);
    }

    /**
     * URL重定向
     * @access protected
     * @param string         $url 跳转的URL表达式
     * @param array|integer  $params 其它URL参数
     * @param integer        $code http code
     * @return void
     */
    protected function redirect($url, $params = [], $code = 302)
    {// 重定向, url 参数 code 网页编码 如 502 什么的
        $response = new Redirect($url);// 返回url 结果集合
        if (is_integer($params)) {// 数字 处理,也就是,可以 转入参数
            $code   = $params;// 很累赘的处理方案
            $params = [];
        }
        $response->code($code)->params($params);//处理对应的 code 跟 返回
        throw new HttpResponseException($response);// 抛出返回异常,然后执行异常,这个牛叉,安逸,这样通过异常强大的中断能力,老刘,不错,不错啊
    }

    /**
     * 获取当前的response 输出类型
     * @access protected
     * @return string
     */
    protected function getResponseType()
    {// 返回 返回类型
        $isAjax = Request::instance()->isAjax();// 看是否是 ajax
        return $isAjax ? Config::get(‘default_ajax_return‘) : Config::get(‘default_return_type‘);
    }// 返回 返回类型配置
}
// 这个部分值得学习的两个地方:
/**
 * 第一:通过对 某一个参数的传入类型的判读,如$params 判读,将 传入参数的 意义进行转义,从 params 编程了 code
 * 第二:利用 php 异常中断的 强大机制,执行 跳转输出
 * 第三:配置项的获取
 *
 * thinkphp5 的自动加载的原则是,给了一个类名,按照这个文件名,找遍所有的目录,加载成功为止
 */


本文出自 “专注php 群号:414194301” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1871668

[李景山php]每天TP5-20161221|thinkphp5-jump.php

标签:thinkphp

原文地址:http://jingshanls.blog.51cto.com/3357095/1871668

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!