ThinkPHP5学习笔记(6)请求和响应

  1. 云栖社区>
  2. 博客>
  3. 正文

ThinkPHP5学习笔记(6)请求和响应

技术小胖子 2017-11-04 18:43:00 浏览603
展开阅读全文

在线视频课程:http://www.kancloud.cn/tpshop/thinkphp5/220692

  • Request请求对象和Response响应对象

  • $_GET、$_POST、$_REQUEST、$_COOKIE

  • 调用请求对象$request=Request::instance()

  • $this->request或request()->url();助手函数

  • input()助手函数

  • 响应格式 json xml 模板渲染

  • 页面跳转 重定向

  • $this->success $this->error $this->redirect



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace app\admin\controller;
 
use think\Request;
use think\Controller;
 
class Index extends Controller
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
    }
 
 
    public function hello(){
        //  获取请求类型
        if(Request::instance()->isGet()) {echo '当前为GET请求';}
        if(Request::instance()->isPost())echo '当前为POST请求';
        if(Request::instance()->isPut()) echo '当前为PUT请求';
        if(Request::instance()->isDelete()) echo '当前为DELETE请求';
        if(Request::instance()->isAjax()) echo '当前为AJAX请求';
        if(Request::instance()->isPjax()) echo '当前为PJAX请求';
        if(Request::instance()->isMobile()) echo '当前为Mobile请求';
        if(Request::instance()->isHead()) echo '当前为HEAD请求';
        if(Request::instance()->isPatch()) echo '当前为PATCH请求';
        if(Request::instance()->isOptions()) echo '当前为OPTIONS请求';
        if(Request::instance()->isCli()) echo '当前为cli';
        if(Request::instance()->isCgi()) echo '当前为cgi';
 
        if(request()->isGet()){echo '当前为GET请求';};
 
 
        // 获取URL信息
 
        $request = Request::instance();
        $request = request();
        echo 'domain:'.$request->domain().'<br/>';
        echo 'file:'.$request->baseFile().'<br/>';
        echo 'url:'.$request->url().'<br/>';
        echo 'url with domain:'.$request->url(true).'<br/>';
        echo 'url without query:'.$request->baseUrl().'<br/>';
        echo 'root:'.$request->root().'<br/>';
        echo 'root with domain:'.$request->root(true).'<br/>';
        echo 'pathinfo:'.$request->pathinfo().'<br/>';
        echo 'pathinfo:'.$request->path().'<br/>';
        echo 'ext'.$request->ext().'<br/>';
 
 
        // 设置/获取 模块/控制器/操作名称
        $request = Request::instance();
        echo '当前模块名称是'.$request->module();
        echo '当前控制器名称是'.$request->controller();
        echo '当前操作名称是'.$request->action();
 
 
        Request::instance()->module('module_name');
        echo '当前模块名称是'.$request->module();
 
        // 获取请求参数
        echo '请求方法:'.$request->method().'<br/>';
        echo '资源类型:'.$request->type().'<br/>';
        echo '访问地址:'.$request->ip().'<br/>';
        echo '是否Ajax请求:'.var_export($request->isAjax(),true);
        echo '请求参数:';
        dump($request->param());
        echo '请求参数:仅包含name';
        dump($request->only(['name']));
        echo '请求参数:排除name';
        dump($request->except(['name']));
 
        // 获取路由和调度信息
 
        echo '路由信息';
        dump($request->route());
 
        echo '调度信息';
        dump($request->dispatch());
 
        // 设置请求信息
        $request = Request::instance();
        $request->root('index.php');
        $request->pathinfo('admin/index/hello');
 
    }
 
    public function hello2(){
        $data = ['name'=>'thinkphp','status'=>1];
        // 浏览器安装json插件
        return json($data);
        // return xml($data);
        // $this->assign('name','渲染 模板');
        // return $this->fetch();
    }
 
 
    // hello3
    // 如果没有 extends Controller 就写:
    // use \traits\controller\Jump;
    public function hello3(){
 
        $this->success('正常的页面跳转','hello');
        $this->error('错误的页面跳转','hello2');
        $this->redirect('http://tp-shop.cn');
    }
 
    public function hello4(){
        // 检测变量是否设置
        Request::instance()->has('id','get');
        Request::instance()->has('name','post');
 
        input('?get.id');
        input('?post.name');
 
        // 变量获取
        $request = Request::instance();
        echo $request->bind('user_name','张三');
        echo $request->user_name;
 
        print_r($request->param());
        echo '<br/>';
        echo $request->param('name');
        print_r(input());
        echo '<br/>';
        echo input('name');
 
        print_r(Request::instance()->param(false));
        print_r(Request::instance()->param(true));
 
 
        echo (input('param.name'));
        print_r (input('param.'));
         // 或
        echo (input('name'));
        print_r('');
 
 
        // 获取GET变量
        echo (Request::instance()->get('id'));
        echo (Request::instance()->get('name'));
        print_r(Request::instance()->get());    // 获取经过过滤的
        print_r(Request::instance()->get(false));//获取原始变量
        // 或
        echo input('get.id');
        echo input('get.name');
        print_r(input('get.'));
 
        // 获取POST变量
        echo (Request::instance()->post('name'));
        print_r(Request::instance()->post());
        print_r(Request::instance()->post(false));
        // 或
        echo (input('post.name'));
        print_r('post.');
 
        // 获取PUT变量
        echo (Request::instance()->put('name'));
        print_r (Request::instance()->put());
        print_r (Request::instance()->put(false));
        // 或
        echo (input('put.name'));
        print_r(input('put.'));
 
        //  获取REQUEST变量
        echo (Request::instance()->request('id'));
        print_r(Request::instance()->request());
        print_r(Request::instance()->request(false));
        // 或
        echo (input('request.id'));
        print_r (input('request.'));
 
        // 获取SERVER变量
        echo (Request::instance()->server('PHP_SELF'));
        print_r(Request::instance()->server());
        // 或
        echo (input('server.PHP_SELF'));
        print_r(input('server.'));
 
 
        //获取SESSION变量
        echo (Request::instance()->session('user_id'));
        print_r(Request::instance()->session());
        // 或
        echo (input('session.user_id'));
        print_r(input('session.'));
 
 
        // 获取Cookie变量
        echo (Request::instance()->cookie('user_id'));
        print_r(Request::instance()->cookie());
        // 或
        echo (input('cookie.user_id'));
        print_r(input('cookie.'));
 
        // 变量过滤
        // 在application/config.php中设置
        // 'default_filter'=>'htmlspecialchars';
        Request::instance()->filter('htmlspecialchars');
        Request::instance()->filter(['strip_tags','htmlspecialchars']);
        // 在添加变量的时候添加过滤方法
        Request::instance()->get('name','','htmlspecialchars');
        Request::instance()->param('username','','strip_tags');
        Request::instance()->post('name','','org\Filter::safeHtml');
 
        Request::instance()->param('username','','strip_tags,strtolower');
 
        Request::instance()->post('email','','FILTER_VALIDATE_EMAIL');
        Request::instance()->post('email','','email');
        Request::instance()->get('name','',false);
 
        // 获取部分变量
        Request::instance()->only('id,name');
        Request::instance()->only(['id','name']);
        Request::instance()->only(['id','name'],'get');
        Request::instance()->only(['id','name'],'post');
 
        // 排除部分变量
        Request::instance()->except('id,name');
        Request::instance()->except(['id','name']);
        Request::instance()->except(['id','name'],'get');
        Request::instance()->except(['id','name'],'post');
 
 
        //  变量修饰符
        input('get.id/d');
        input('get.name/s');
        input('get.ids/a');
        Request::instance()->get('id/d');
 
    }
 
    public function hello5(){
        Request::instance()->get(['id'=>10]);
        Request::instance()->post(['name'=>'thinkphp']);
        // 尽量避免直接修改$_GET,$_POST,也不能直接修改param变量,下面写法无效
        // Request::instance()->param(['id'=>10]);
 
    }
 
 
}                    本文转自phpervip 51CTO博客,原文链接:http://blog.51cto.com/phpervip/1931987,如需转载请自行联系原作者


网友评论

登录后评论
0/500
评论
技术小胖子
+ 关注