TP5请求对象Request

第一种方式, 助手函数 request()

第二种是, think\request 类来获取实例

use think\Request;

class Index{

    public function index(){
        $request = Request::instance();  //单例模式
    }

}

第三种方法是, 注入对象, 建议使用这种方式.

use think\Request;

class Index
{
    public function del1(Request $request)
    {
        # 获取浏览器输入框的值
        # 路径都为 http://bd.demo1.com/admin.php/manager/del1/type/5.html?id=10
        /*
         * 1. 获取域名
         * string(19) "http://bd.demo1.com"
         */
        dump($request->domain());
        /*
         * 2. 获取到 .html的部分
         * string(24) "manager/del1/type/5.html"
         */
        dump($request->pathinfo());
        /*
         * 3. path方法, 获取真实的不含有.html的部分
         * string(19) "manager/del1/type/5"
         */
        dump($request->path());
        /*
         * 4. 请求类型 method() 方法
         * string(3) "GET"
         */
        dump($request->method());
        /*
         * 5. 快捷请求判断
         * isGet()  isPost()  isAjax() 等
         * bool(true)
         */
        dump($request->isGet());
        /*
         * 6. 请求的参数
         *      array(1) {
         *          ["id"] => string(2) "10"
         *      }
         *  tp5.0以后 get不包含pathinfo中的传值, 这里获取不到type=5
         *  如果想获取pathinfo中的值, 可以用 param() 来获取
         *      array(2) {
         *           ["id"] => string(2) "10"
         *           ["type"] => string(1) "5"
         *       }
         */
        dump($request->get());
        dump($request->param());
        dump($request->post());
        /*
          7. 获取session中的值
            array(1) {
              ["loginer"] => array(12) {
                ["mg_id"] => int(25)
                ["mg_name"] => string(2) "zq"
                ["mg_tname"] => string(2) "zq"
                ["mg_pwd"] => string(9) "zl4921272"
                ["mg_pid"] => int(0)
                ["mg_path"] => string(0) ""
                ["mg_level"] => int(0)
                ["mg_xm"] => string(0) ""
                ["mg_xm_ch"] => string(3) "all"
                ["mg_time"] => string(19) "2018-06-11 16:18:43"
                ["mg_last_time"] => string(19) "2019-01-08 17:01:32"
                ["mg_role_id"] => int(52)
              }
            }
         */
        dump($request->session());
        /*
         * 8. 获取cookie中的值
         */
        dump($request->cookie());

        /*
         * 9. 如果想直接获取到一个值, 上面获取的都是数组.
         * get/param/post/session/cookie 都支持这种方式获取到一个具体值
         * 5
         */
        dump($request->param('type'));
        dump($request->cookie('email'));

        /*
         * 控制器中获取
         * 模块    $request->module()
         * 控制器   $request->controller()
         * 操作    $request->action()
         */
        dump($request->module());
        dump($request->controller());
        dump($request->action());

        /*
         * url方法
         * string(41) "/admin.php/manager/del1/type/5.html?id=10"
         * string(35) "/admin.php/manager/del1/type/5.html"
         */
        dump($request->url());
        dump($request->baseurl());
    }

}

------------------------------------------------------------------------------------------------------------

助手函数 input

// input助手函数
public function input(Request $request)
{
    # 路径都为 http://bd.demo1.com/admin.php/manager/input/type/5.html?id=10
    # input 第二个参数为默认值, 第三个参数为过滤,类型装换
    $res = input('post.id', 100, 'intval');
    dump($request->post('id', 100, 'intval'));

    dump($res);
}

------------------------------------------------------------------------------------------------------------

响应对象 Response

// response
public function del02()
{
    $res = [
        'code'   => 200,
        'result' => [
            'list' => [1, 2, 3, 4, 5, 6]
        ]
    ];
    Config::set('default_return_type', 'json');
    return $res;
}

如果是给一个 app返回json数据, 那么可以定义一个模块, 在模块配置中, 将如下设置为json, 则 return返回值直接为json.

// 默认输出类型
'default_return_type'    => 'json',

上述方法, 所有的接口都会返回json, 如果不想这么做, 可以用如下方法做一个判断, 

public function getUserInfo($type="json")
{
    if(!in_array($type, ['json', 'xml']))
    {
        $type='json';    
    }
    Config::set('default_return_type', $type);

    $data = [
        'code' => 200,
        'result' => [
            'username'  => 'wu',
            'useremail' => '[email protected]'
        ]
    ];
    
    return $data;
}
发布了88 篇原创文章 · 获赞 6 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/codipy/article/details/100007332