企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 快速入门(三):请求和响应 本章主要了解如何获取当前的请求信息,以及进行不同的输出响应、跳转和页面重定向,主要内容包括: - - [请求对象](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u8BF7u6C42u5BF9u8C61) - [传统方式调用](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u4F20u7EDFu65B9u5F0Fu8C03u7528) - [继承think\\Controller](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#-think-controller) - [自动注入请求对象](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u81EAu52A8u6CE8u5165u8BF7u6C42u5BF9u8C61) - [动态绑定属性](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u52A8u6001u7ED1u5B9Au5C5Eu6027) - [使用助手函数](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u4F7Fu7528u52A9u624Bu51FDu6570) - [请求信息](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u8BF7u6C42u4FE1u606F) - [获取请求变量](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u83B7u53D6u8BF7u6C42u53D8u91CF) - [获取请求参数](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u83B7u53D6u8BF7u6C42u53C2u6570) - [获取URL信息](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#-url-) - [获取当前模块/控制器/操作信息](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#--3) - [获取路由和调度信息](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u83B7u53D6u8DEFu7531u548Cu8C03u5EA6u4FE1u606F) - [响应对象](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u54CDu5E94u5BF9u8C61) - [自动输出](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u81EAu52A8u8F93u51FA) - [手动输出](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u624Bu52A8u8F93u51FA) - [页面跳转](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u9875u9762u8DF3u8F6C) - [页面重定向](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u9875u9762u91CDu5B9Au5411) - [总结](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/145255#u603Bu7ED3) `ThinkPHP5`的架构设计和之前版本的主要区别之一就在于增加了`Request`请求对象和`Response`响应对象的概念,了解了这两个对象的作用和用法对你的应用开发非常关键。 ## 请求对象 > `Request`对象的作用是与客户端交互,收集客户端的Form、Cookies、超链接,或者收集服务器端的环境变量。 > > Request对象是从客户端向服务器发出请求,包括用户提交的信息以及客户端的一些信息。客户端可通过HTML表单或在网页地址后面提供参数的方法提交数据,然后通过Request对象的相关方法来获取这些数据。Request的各种方法主要用来处理客户端浏览器提交的请求中的各项参数和选项。 `ThinkPHP5`的`Request`对象由`think\Request`类完成。 `Request`对象的一个主要职责是统一和更安全地获取当前的请求信息,你需要避免直接操作`$_GET`、`$_POST`、`$_REQUEST`、`$_SESSION`、`$_COOKIE`,甚至`$_FILES`等全局变量,而是统一使用`Request`对象提供的方法来获取请求变量。 下面来举一个最简单的例子(获取当前访问的url地址)来说明在不同的情况下如何调用`Request`对象。 ### 传统方式调用 该用法主要是用来告诉大家`Request`对象是如何实例化的,因为实际开发中很少选择这种方式调用。 ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(<span class="hljs-regexp">$name</span> = <span class="hljs-string">'World'</span>)</span></span>{ <span class="hljs-regexp">$request</span> = Request::instance(); <span class="hljs-comment">// 获取当前URL地址 不含域名</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'url: '</span> . <span class="hljs-regexp">$request</span>->url() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,'</span> . <span class="hljs-regexp">$name</span> . <span class="hljs-string">'!'</span>; } }</span> ``` ``` 访问下面的URL地址: ``` <pre class="calibre18"> ``` http:<span class="hljs-regexp">//tp</span>5.com/<span class="hljs-keyword">index</span>/<span class="hljs-keyword">index</span>/hello.html?name=thinkphp ``` ``` 页面输出结果为: ``` <pre class="calibre18"> ``` <span class="hljs-regexp">url</span>: <span class="hljs-string">/index/index/hello.html?name=thinkphp</span><span class="hljs-regexp">Hello,thinkphp!</span> ``` ``` ### 继承think\\Controller 如果控制器类继承了`think\Controller`的话,可以做如下简化调用: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(<span class="hljs-regexp">$name</span> = <span class="hljs-string">'World'</span>)</span></span>{ <span class="hljs-comment">// 获取当前URL地址 不含域名</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'url: '</span> . <span class="hljs-regexp">$this</span>->request->url() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,'</span> . <span class="hljs-regexp">$name</span> . <span class="hljs-string">'!'</span>; } }</span> ``` ``` ### 自动注入请求对象 如果没有继承`think\Controller`,则可以使用`Request`对象注入的方式来简化调用: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>, <span class="hljs-regexp">$name</span> = <span class="hljs-string">'World'</span>)</span></span>{ <span class="hljs-comment">// 获取当前URL地址 不含域名</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'url: '</span> . <span class="hljs-regexp">$request</span>->url() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,'</span> . <span class="hljs-regexp">$name</span> . <span class="hljs-string">'!'</span>; } }</span> ``` ``` > #### 提示: > > - - - - - - > > hello方法的request参数是系统自动注入的,而不需要通过URL请求传入。 ### 动态绑定属性 可以给Request请求对象绑定属性,方便全局调用,例如我们可以在公共控制器中绑定当前登录的用户模型到请求对象: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>\<span class="hljs-title">User</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Session</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Base</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_initialize</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = User::get(Session::get(<span class="hljs-string">'user_id'</span>)); Request::instance()->bind(<span class="hljs-string">'user'</span>,<span class="hljs-regexp">$user</span>); } }</span> ``` ``` 然后,在其它的控制器中就可以直接使用 ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>\<span class="hljs-title">Base</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>)</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$request</span>->user->id; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$request</span>->user->name; } }</span> ``` ``` ### 使用助手函数 如果既没有继承`think\Controller`也不想给操作方法添加额外的`Request`对象参数,那么也可以使用系统提供的助手,例如: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(<span class="hljs-regexp">$name</span> = <span class="hljs-string">'World'</span>)</span></span>{ <span class="hljs-comment">// 获取当前URL地址 不含域名</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'url: '</span> . request()->url() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,'</span> . <span class="hljs-regexp">$name</span> . <span class="hljs-string">'!'</span>; } }</span> ``` ``` 上面任意一种方式都可以调用当前请求的`Request`对象实例,然后通过请求对象实例的方法来完成不同的信息获取或者设置。 > 在后面的示例中,如果没有特别说明,我们均以第三种方式,自动注入请求对象的方式来讲解,请注意根据自己的调用方式灵活运用。 ## 请求信息 我们来陆续了解下请求对象的常用方法。 ### 获取请求变量 系统推荐使用`param`方法统一获取当前请求变量,该方法最大的优势是让你不需要区分当前请求类型而使用不同的全局变量或者方法,并且可以满足大部分的参数需求,下面举一个例子。 ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>)</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">'请求参数:'</span>; dump(<span class="hljs-regexp">$request</span>->param()); <span class="hljs-keyword">echo</span> <span class="hljs-string">'name:'</span>.<span class="hljs-regexp">$request</span>->param(<span class="hljs-string">'name'</span>); } }</span> ``` ``` 访问下面的URL地址: ``` <pre class="calibre18"> ``` http:<span class="hljs-regexp">//tp</span>5.com/<span class="hljs-keyword">index</span>/<span class="hljs-keyword">index</span>/hello.html?test=ddd&name=thinkphp ``` ``` 页面输出结果为: ``` <pre class="calibre18"> ``` 请求参数: <span class="hljs-keyword">array</span> (size=<span class="hljs-number">2</span>) <span class="hljs-string">'test'</span> => string <span class="hljs-string">'ddd'</span> (length=<span class="hljs-number">3</span>) <span class="hljs-string">'name'</span> => string <span class="hljs-string">'thinkphp'</span> (length=<span class="hljs-number">8</span>) name:thinkphp ``` ``` 系统提供了一个input助手函数来简化`Request`对象的param方法,用法如下: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">'请求参数:'</span>; dump(input()); <span class="hljs-keyword">echo</span> <span class="hljs-string">'name:'</span>.input(<span class="hljs-string">'name'</span>); } }</span> ``` ``` `param`方法获取的参数会自动判断当前的请求,以`POST`请求为例的话,参数的优先级别为: #### 路由变量 > 当前请求变量($\_POST变量) > $\_GET变量 > #### 注意: > > - - - - - - > > 这里的路由变量指的是路由规则里面定义的变量或者`PATH_INFO`地址中的变量。路由变量无法使用`get`方法或者`$_GET`变量获取。 `param`方法支持变量的过滤和默认值,例如: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>)</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">'name:'</span>.<span class="hljs-regexp">$request</span>->param(<span class="hljs-string">'name'</span>,<span class="hljs-string">'World'</span>,<span class="hljs-string">'strtolower'</span>); <span class="hljs-keyword">echo</span> <span class="hljs-string">'<br/>test:'</span>.<span class="hljs-regexp">$request</span>->param(<span class="hljs-string">'test'</span>,<span class="hljs-string">'thinkphp'</span>,<span class="hljs-string">'strtoupper'</span>); } }</span> ``` ``` 访问下面的URL地址: ``` <pre class="calibre18"> ``` http:<span class="hljs-regexp">//tp</span>5.com/<span class="hljs-keyword">index</span>/<span class="hljs-keyword">index</span>/hello.html?test=ddd&name=thinkphp ``` ``` 页面输出结果为: ``` <pre class="calibre18"> ``` <span class="hljs-string">name:</span>thinkphp <span class="hljs-string">test:</span>DDD ``` ``` 可以设置全局的过滤方法,如下: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 设置默认的全局过滤规则 多个用数组或者逗号分隔</span><span class="hljs-string">'default_filter'</span> => <span class="hljs-string">'htmlspecialchars '</span>, ``` ``` 除了`Param`方法之外,`Request`对象也可以用于获取其它的输入参数,例如: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>)</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">'GET参数:'</span>; dump(<span class="hljs-regexp">$request</span>->get()); <span class="hljs-keyword">echo</span> <span class="hljs-string">'GET参数:name'</span>; dump(<span class="hljs-regexp">$request</span>->get(<span class="hljs-string">'name'</span>)); <span class="hljs-keyword">echo</span> <span class="hljs-string">'POST参数:name'</span>; dump(<span class="hljs-regexp">$request</span>->post(<span class="hljs-string">'name'</span>)); <span class="hljs-keyword">echo</span> <span class="hljs-string">'cookie参数:name'</span>; dump(<span class="hljs-regexp">$request</span>->cookie(<span class="hljs-string">'name'</span>)); <span class="hljs-keyword">echo</span> <span class="hljs-string">'上传文件信息:image'</span>; dump(<span class="hljs-regexp">$request</span>->file(<span class="hljs-string">'image'</span>)); } }</span> ``` ``` 页面输出的结果为: ``` <pre class="calibre18"> ``` GET参数: array (size=<span class="hljs-number">1</span>) <span class="hljs-string">'name'</span> => <span class="hljs-keyword">string</span> <span class="hljs-string">'thinkphp'</span> (length=<span class="hljs-number">8</span>) GET参数:name <span class="hljs-keyword">string</span> <span class="hljs-string">'thinkphp'</span> (length=<span class="hljs-number">8</span>) POST参数:name <span class="hljs-keyword">null</span> 当前请求(自动识别请求类型)的参数:name <span class="hljs-keyword">string</span> <span class="hljs-string">'thinkphp'</span> (length=<span class="hljs-number">8</span>) cookie参数:name <span class="hljs-keyword">null</span> 上传文件信息:image <span class="hljs-keyword">null</span> ``` ``` 使用助手函数的代码为: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">'GET参数:'</span>; dump(input(<span class="hljs-string">'get.'</span>)); <span class="hljs-keyword">echo</span> <span class="hljs-string">'GET参数:name'</span>; dump(input(<span class="hljs-string">'get.name'</span>)); <span class="hljs-keyword">echo</span> <span class="hljs-string">'POST参数:name'</span>; dump(input(<span class="hljs-string">'post.name'</span>)); <span class="hljs-keyword">echo</span> <span class="hljs-string">'cookie参数:name'</span>; dump(input(<span class="hljs-string">'cookie.name'</span>)); <span class="hljs-keyword">echo</span> <span class="hljs-string">'上传文件信息:image'</span>; dump(input(<span class="hljs-string">'file.image'</span>)); } }</span> ``` ``` 获取变量的方法包括: 方法 作用 param 获取请求变量 get 获取$\_GET变量 post 获取$\_POST变量 put 获取PUT请求变量 delete 获取DELETE请求变量 patch 获取PATCH请求变量 request 获取$\_REQUEST变量 route 获取路由(URL)变量 session 获取$\_SESSION变量 cookie 获取$\_COOKIE变量 server 获取$\_SERVER变量 env 获取$\_ENV变量 file 获取上传文件信息> 除了file方法之外,其它方法都支持默认值和过滤方法。 ### 获取请求参数 把`Hello`方法改为如下: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>)</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">'请求方法:'</span> . <span class="hljs-regexp">$request</span>->method() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">'资源类型:'</span> . <span class="hljs-regexp">$request</span>->type() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">'访问IP:'</span> . <span class="hljs-regexp">$request</span>->ip() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">'是否AJax请求:'</span> . var_export(<span class="hljs-regexp">$request</span>->isAjax(), <span class="hljs-keyword">true</span>) . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">'请求参数:'</span>; dump(<span class="hljs-regexp">$request</span>->param()); <span class="hljs-keyword">echo</span> <span class="hljs-string">'请求参数:仅包含name'</span>; dump(<span class="hljs-regexp">$request</span>->only([<span class="hljs-string">'name'</span>])); <span class="hljs-keyword">echo</span> <span class="hljs-string">'请求参数:排除name'</span>; dump(<span class="hljs-regexp">$request</span>->except([<span class="hljs-string">'name'</span>])); } }</span> ``` ``` 访问下面的URL地址: ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span>/<span class="hljs-regexp">/tp5.com/index</span><span class="hljs-regexp">/index/hello</span><span class="hljs-regexp">/test/ddd</span>.html?name=thinkphp ``` ``` 页面输出结果为: ``` <pre class="calibre18"> ``` 请求方法:GET 资源类型:html 访问地址:127.0.<span class="hljs-operator">0.<span class="hljs-number">1</span> 是否AJax请求:<span class="hljs-number">false</span> 请求参数: <span class="hljs-number">array</span> (<span class="hljs-keyword">size</span>=<span class="hljs-number">2</span>) <span class="hljs-string">'test'</span> => <span class="hljs-keyword">string</span> <span class="hljs-string">'ddd'</span> (<span class="hljs-keyword">length</span>=<span class="hljs-number">3</span>) <span class="hljs-string">'name'</span> => <span class="hljs-keyword">string</span> <span class="hljs-string">'thinkphp'</span> (<span class="hljs-keyword">length</span>=<span class="hljs-number">8</span>) 请求参数:仅包含<span class="hljs-keyword">name</span><span class="hljs-number">array</span> (<span class="hljs-keyword">size</span>=<span class="hljs-number">1</span>) <span class="hljs-string">'name'</span> => <span class="hljs-keyword">string</span> <span class="hljs-string">'thinkphp'</span> (<span class="hljs-keyword">length</span>=<span class="hljs-number">8</span>) 请求参数:排除<span class="hljs-keyword">name</span><span class="hljs-number">array</span> (<span class="hljs-keyword">size</span>=<span class="hljs-number">1</span>) <span class="hljs-string">'test'</span> => <span class="hljs-keyword">string</span> <span class="hljs-string">'ddd'</span> (<span class="hljs-keyword">length</span>=<span class="hljs-number">3</span>)</span> ``` ``` ### 获取URL信息 ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>,<span class="hljs-regexp">$name</span> = <span class="hljs-string">'World'</span>)</span></span>{ <span class="hljs-comment">// 获取当前域名</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'domain: '</span> . <span class="hljs-regexp">$request</span>->domain() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取当前入口文件</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'file: '</span> . <span class="hljs-regexp">$request</span>->baseFile() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取当前URL地址 不含域名</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'url: '</span> . <span class="hljs-regexp">$request</span>->url() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取包含域名的完整URL地址</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'url with domain: '</span> . <span class="hljs-regexp">$request</span>->url(<span class="hljs-keyword">true</span>) . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取当前URL地址 不含QUERY_STRING</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'url without query: '</span> . <span class="hljs-regexp">$request</span>->baseUrl() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取URL访问的ROOT地址</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'root:'</span> . <span class="hljs-regexp">$request</span>->root() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取URL访问的ROOT地址</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'root with domain: '</span> . <span class="hljs-regexp">$request</span>->root(<span class="hljs-keyword">true</span>) . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取URL地址中的PATH_INFO信息</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'pathinfo: '</span> . <span class="hljs-regexp">$request</span>->pathinfo() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取URL地址中的PATH_INFO信息 不含后缀</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'pathinfo: '</span> . <span class="hljs-regexp">$request</span>->path() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-comment">// 获取URL地址中的后缀信息</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'ext: '</span> . <span class="hljs-regexp">$request</span>->ext() . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,'</span> . <span class="hljs-regexp">$name</span> . <span class="hljs-string">'!'</span>; } }</span> ``` ``` 访问下面的URL地址: ``` <pre class="calibre18"> ``` http:<span class="hljs-regexp">//tp</span>5.com/<span class="hljs-keyword">index</span>/<span class="hljs-keyword">index</span>/hello.html?name=thinkphp ``` ``` 页面输出结果为: ``` <pre class="calibre18"> ``` <span class="hljs-string">domain:</span> <span class="hljs-string">http:</span><span class="hljs-comment">//tp5.com</span><span class="hljs-string">file:</span> /index.php <span class="hljs-string">url:</span> <span class="hljs-regexp">/index/</span>index/hello.html?name=thinkphp url with domain: <span class="hljs-string">http:</span><span class="hljs-comment">//tp5.com/index/index/hello.html?name=thinkphp</span> url without <span class="hljs-string">query:</span> <span class="hljs-regexp">/index/</span>index/hello.html <span class="hljs-string">root:</span> root with <span class="hljs-string">domain:</span> <span class="hljs-string">http:</span><span class="hljs-comment">//tp5.com</span><span class="hljs-string">pathinfo:</span> index<span class="hljs-regexp">/index/</span>hello.html <span class="hljs-string">pathinfo:</span> index<span class="hljs-regexp">/index/</span>hello <span class="hljs-string">ext:</span> html Hello,thinkphp! ``` ``` URL请求和信息方法可以总结如下: 方法 作用 domain 获取当前的域名 url 获取当前的完整URL地址 baseUrl 获取当前的URL地址,不含QUERY\_STRING baseFile 获取当前的SCRIPT\_NAME root 获取当前URL的root地址 pathinfo 获取当前URL的pathinfo地址 path 获取当前URL的pathinfo地址,不含后缀 ext 获取当前URL的后缀 type 获取当前请求的资源类型 scheme 获取当前请求的scheme query 获取当前URL地址的QUERY\_STRING host 获取当前URL的host地址 port 获取当前URL的port号 protocol 获取当前请求的SERVER\_PROTOCOL remotePort 获取当前请求的REMOTE\_PORT> url、baseUrl、baseFile、root方法如果传入true,表示获取包含域名的地址。 ### 获取当前模块/控制器/操作信息 hello方法修改如下: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>, <span class="hljs-regexp">$name</span> = <span class="hljs-string">'World'</span>)</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">'模块:'</span>.<span class="hljs-regexp">$request</span>->module(); <span class="hljs-keyword">echo</span> <span class="hljs-string">'<br/>控制器:'</span>.<span class="hljs-regexp">$request</span>->controller(); <span class="hljs-keyword">echo</span> <span class="hljs-string">'<br/>操作:'</span>.<span class="hljs-regexp">$request</span>->action(); } ``` ``` 访问URL地址: ``` <pre class="calibre18"> ``` http:<span class="hljs-regexp">//tp</span>5.com/<span class="hljs-keyword">index</span>/<span class="hljs-keyword">index</span>/hello.html?test=ddd&name=thinkphp ``` ``` 页面会显示: ``` <pre class="calibre18"> ``` 模块:<span class="hljs-keyword">index</span> 控制器:<span class="hljs-keyword">Index</span> 操作:hello ``` ``` > controller方法获取的是驼峰命名的实际的控制器名,其它都是小写返回。 ### 获取路由和调度信息 hello方法修改如下: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>, <span class="hljs-regexp">$name</span> = <span class="hljs-string">'World'</span>)</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">'路由信息:'</span>; dump(<span class="hljs-regexp">$request</span>->routeInfo()); <span class="hljs-keyword">echo</span> <span class="hljs-string">'调度信息:'</span>; dump(<span class="hljs-regexp">$request</span>->dispatch()); <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,'</span> . <span class="hljs-regexp">$name</span> . <span class="hljs-string">'!'</span>; } }</span> ``` ``` 然后在配置文件中添加路由定义为: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">return</span> [ <span class="hljs-string">'hello/:name'</span> =>[<span class="hljs-string">'index/hello'</span>,[],[<span class="hljs-string">'name'</span>=><span class="hljs-string">'\w+'</span>]], ]; ``` ``` 访问下面的URL地址: ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/hello/thinkphp</span> ``` ``` 页面输出信息为: ``` <pre class="calibre18"> ``` 路由信息: <span class="hljs-keyword">array</span> (size=<span class="hljs-number">4</span>) <span class="hljs-string">'rule'</span> => string <span class="hljs-string">'hello/:name'</span> (length=<span class="hljs-number">11</span>) <span class="hljs-string">'route'</span> => string <span class="hljs-string">'index/hello'</span> (length=<span class="hljs-number">11</span>) <span class="hljs-string">'pattern'</span> => <span class="hljs-keyword">array</span> (size=<span class="hljs-number">1</span>) <span class="hljs-string">'name'</span> => string <span class="hljs-string">'\w+'</span> (length=<span class="hljs-number">3</span>) <span class="hljs-string">'option'</span> => <span class="hljs-keyword">array</span> (size=<span class="hljs-number">0</span>) <span class="hljs-keyword">empty</span> 调度信息: <span class="hljs-keyword">array</span> (size=<span class="hljs-number">2</span>) <span class="hljs-string">'type'</span> => string <span class="hljs-string">'module'</span> (length=<span class="hljs-number">6</span>) <span class="hljs-string">'module'</span> => <span class="hljs-keyword">array</span> (size=<span class="hljs-number">3</span>) <span class="hljs-number">0</span> => <span class="hljs-keyword">null</span><span class="hljs-number">1</span> => string <span class="hljs-string">'index'</span> (length=<span class="hljs-number">5</span>) <span class="hljs-number">2</span> => string <span class="hljs-string">'hello'</span> (length=<span class="hljs-number">5</span>) Hello,thinkphp! ``` ``` ## 响应对象 > `Response`对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应。通常用于输出数据给客户端或者浏览器。 `ThinkPHP5`的`Response`响应对象由`think\Response`类或者子类完成,下面介绍下基本的用法。 ### 自动输出 大多数情况,我们不需要关注`Response`对象本身,只需要在控制器的操作方法中返回数据即可,系统会根据`default_return_type`和`default_ajax_return`配置决定响应输出的类型。 默认的自动响应输出会自动判断是否`AJAX`请求,如果是的话会自动输出`default_ajax_return`配置的输出类型。 ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$data</span> = [<span class="hljs-string">'name'</span> => <span class="hljs-string">'thinkphp'</span>, <span class="hljs-string">'status'</span> => <span class="hljs-string">'1'</span>]; <span class="hljs-keyword">return</span> <span class="hljs-regexp">$data</span>; } }</span> ``` ``` 由于默认是输出`Html`输出,所以访问页面输出结果为: ![](https://img.kancloud.cn/83/88/8388a281e2f03fd0551ba92f9ddffe9f_300x63.png) 修改配置文件,添加: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 默认输出类型</span><span class="hljs-string">'default_return_type'</span> => <span class="hljs-string">'json'</span>, ``` ``` 再次访问的输出结果为: ``` <pre class="calibre18"> ``` {"<span class="hljs-regexp">name</span>":<span class="hljs-string"><span class="hljs-operator">"thinkphp"</span></span>,"<span class="hljs-regexp">status</span>":<span class="hljs-string"><span class="hljs-operator">"1"</span></span>} ``` ``` 修改输出类型为xml: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 默认输出类型</span><span class="hljs-string">'default_return_type'</span> => <span class="hljs-string">'xml'</span>, ``` ``` 则输出结果变成: ``` <pre class="calibre18"> ``` <span class="hljs-regexp"><<span class="hljs-operator">think</span>></span><span class="hljs-regexp"><<span class="hljs-operator">name</span>></span>thinkphp<span class="hljs-regexp"></<span class="hljs-operator">name</span>></span><span class="hljs-regexp"><<span class="hljs-operator">status</span>></span>1<span class="hljs-regexp"></<span class="hljs-operator">status</span>></span><span class="hljs-regexp"></<span class="hljs-operator">think</span>></span> ``` ``` ### 手动输出 在必要的时候,可以手动控制输出类型和参数,这种方式较为灵活。如果需要指定输出类型,可以通过下面的方式: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$data</span> = [<span class="hljs-string">'name'</span> => <span class="hljs-string">'thinkphp'</span>, <span class="hljs-string">'status'</span> => <span class="hljs-string">'1'</span>]; <span class="hljs-keyword">return</span> json(<span class="hljs-regexp">$data</span>); } }</span> ``` ``` 无论配置参数如何设置,页面输出的结果为: ``` <pre class="calibre18"> ``` {"<span class="hljs-regexp">name</span>":<span class="hljs-string"><span class="hljs-operator">"thinkphp"</span></span>,"<span class="hljs-regexp">status</span>":<span class="hljs-string"><span class="hljs-operator">"1"</span></span>} ``` ``` 默认的情况下发送的http状态码是`200`,如果需要返回其它的状态码,可以使用: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$data</span> = [<span class="hljs-string">'name'</span> => <span class="hljs-string">'thinkphp'</span>, <span class="hljs-string">'status'</span> => <span class="hljs-string">'1'</span>]; <span class="hljs-keyword">return</span> json(<span class="hljs-regexp">$data</span>, <span class="hljs-number">201</span>); } }</span> ``` ``` 或者发送更多的响应头信息: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$data</span> = [<span class="hljs-string">'name'</span> => <span class="hljs-string">'thinkphp'</span>, <span class="hljs-string">'status'</span> => <span class="hljs-string">'1'</span>]; <span class="hljs-keyword">return</span> json(<span class="hljs-regexp">$data</span>, <span class="hljs-number">201</span>, [<span class="hljs-string">'Cache-control'</span> => <span class="hljs-string">'no-cache,must-revalidate'</span>]); } }</span> ``` ``` 也支持使用下面的方式: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$data</span> = [<span class="hljs-string">'name'</span> => <span class="hljs-string">'thinkphp'</span>, <span class="hljs-string">'status'</span> => <span class="hljs-string">'1'</span>]; <span class="hljs-keyword">return</span> json(<span class="hljs-regexp">$data</span>)->code(<span class="hljs-number">201</span>)->header([<span class="hljs-string">'Cache-control'</span> => <span class="hljs-string">'no-cache,must-revalidate'</span>]); } }</span> ``` ``` 默认支持的输出类型包括: 输出类型 快捷方法 渲染模板输出 view JSON输出 json JSONP输出 jsonp XML输出 xml 页面重定向 redirect所以,同样的可以使用`xml`方法输出`XML`数据类型: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$data</span> = [<span class="hljs-string">'name'</span> => <span class="hljs-string">'thinkphp'</span>, <span class="hljs-string">'status'</span> => <span class="hljs-string">'1'</span>]; <span class="hljs-keyword">return</span> xml(<span class="hljs-regexp">$data</span>, <span class="hljs-number">201</span>); } }</span> ``` ``` ### 页面跳转 如果需要进行一些简单的页面操作提示或者重定向,可以引入`traits\controller\Jump`,就可以使用相关页面跳转和重定向方法,下面举一个简单的例子,当页面传入name参数为thinkphp的时候,跳转到欢迎页面,其它情况则跳转到一个guest页面。 ``` <pre class="calibre18"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">use</span> \<span class="hljs-title">traits</span>\<span class="hljs-title">controller</span>\<span class="hljs-title">Jump</span>; <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">(<span class="hljs-regexp">$name</span>=<span class="hljs-string">''</span>)</span></span>{ <span class="hljs-keyword">if</span> (<span class="hljs-string">'thinkphp'</span> == <span class="hljs-regexp">$name</span>) { <span class="hljs-regexp">$this</span>->success(<span class="hljs-string">'欢迎使用ThinkPHP 5.0'</span>,<span class="hljs-string">'hello'</span>); } <span class="hljs-keyword">else</span> { <span class="hljs-regexp">$this</span>->error(<span class="hljs-string">'错误的name'</span>,<span class="hljs-string">'guest'</span>); } } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,ThinkPHP!'</span>; } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">guest</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,Guest!'</span>; } } ``` ``` > 这里我们使用 > > ``` > <pre class="calibre25"> > ``` > > <span class="hljs-operator"><span class="hljs-keyword">use</span> \traits\controller\Jump;</span> > > ``` > > ``` > > 引入了一个`Jump` trait,这是`PHP5.4`版本的新特性,如果你的控制器类是继承的`\think\Controller`的话,系统已经自动为你引入了 `\traits\controller\Jump`,无需再次引入。 当我们访问 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com</span> ``` ``` 页面显示如图所示: ![](http://box.kancloud.cn/2016-07-04_577a788e5feb9.png) 然后显示 `Hello,Guest!`。 如果访问URL地址为: ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com?name=thinkphp</span> ``` ``` 则页面显示如图所示: ![](https://img.kancloud.cn/9c/cf/9ccf1ac302b0b8bd07a51e673f952aea_490x251.png) 然后显示 `Hello,ThinkPHP!`。 ### 页面重定向 如果要进行页面重定向跳转,可以使用: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">use</span> \<span class="hljs-title">traits</span>\<span class="hljs-title">controller</span>\<span class="hljs-title">Jump</span>; <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">(<span class="hljs-regexp">$name</span>=<span class="hljs-string">''</span>)</span></span>{ <span class="hljs-keyword">if</span> (<span class="hljs-string">'thinkphp'</span> == <span class="hljs-regexp">$name</span>) { <span class="hljs-regexp">$this</span>->redirect(<span class="hljs-string">'http://thinkphp.cn'</span>); } <span class="hljs-keyword">else</span> { <span class="hljs-regexp">$this</span>->success(<span class="hljs-string">'欢迎使用ThinkPHP'</span>,<span class="hljs-string">'hello'</span>); } } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,ThinkPHP!'</span>; } } ``` ``` 当我们再次访问 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com?name=thinkphp</span> ``` ``` 就会重定向到ThinkPHP官网 `http://thinkphp.cn` > `redirect`方法默认使用`302`跳转,如不需要可以使用第二个参数进行301跳转。 ``` <pre class="calibre18"> ``` $<span class="hljs-keyword">this</span>->redirect(<span class="hljs-operator">'http</span>:<span class="hljs-comment">//thinkphp.cn',301);</span> ``` ``` 在任何时候(即使没有引入Jump trait的话),我们可以使用系统提供的助手函数redirect函数进行重定向。 ``` <pre class="calibre18"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">(<span class="hljs-regexp">$name</span>=<span class="hljs-string">''</span>)</span></span>{ <span class="hljs-keyword">if</span> (<span class="hljs-string">'thinkphp'</span> == <span class="hljs-regexp">$name</span>) { <span class="hljs-keyword">return</span> redirect(<span class="hljs-string">'http://thinkphp.cn'</span>); } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello,ThinkPHP!'</span>; } } } ``` ``` ## 总结 本章我们了解了`Request`请求对象和`Response`响应对象的基本用法,以及如何更轻松的获取请求变量。