recess----2.Controller里面取用request信息
事实上,第一个APP里面除了没有model,其它的都有用过了,但是需要单独拎出来看看清楚。
Recess框架里面的controller就是一个典型的MVC框架的controller,它负责处理从浏览器(或其它客户端)过来的request,然后调用引用model里面的方法来进行运算,最后选择合适的view来产生response,然后把response发回给客户端。这里没有什么好纠结的,深入的得去看代码了。
这里的过程,我们继续在Hello Recess这一个APP的基础上来进行。
不妨再回来看看第一个例子里面的controller:
<?php
Library::import('recess.framework.controllers.Controller');
/**
* !RespondsWith Layouts
* !Prefix Views: home/, Routes: /
*/
class HelloRecessHomeController extends Controller { /** !Route GET */
function index() {
$this->flash = 'Hello Recess!';
}
}
?>
前面有大概介绍,这里的注释“!Route GET”是做routing来用的。什么实现啊,效率啊,咱暂时不关心,那是后面的话题,咱还是继续讨论controller。controller都处理些什么呢?OK,上面的例子处理的就是浏览器发来的GET请求。我们知道,HTTP的标准请求包括了request METHOD,variables, headers, cookies, URL等等这些。而注释里的“!Route”就是告诉routing,我们用controller里面的这个method(这里指contorller这个类里面的一个函数,也就是方法)来处理相应的“GET/POST/PUT/DELETE”这些HTTP METHOD(这里指HTTP协议里面定义的请求方式)。然后,我们可以通过一系列借口来取出request携带的信息并进行处理。
以GET请求为例,我们看看怎样取出信息。我们需要再给这个controller添加一个method:
/** !Route GET, printIt */
function printIt() {
print $this->request->resource . '<br />';
print_r($this->request);
exit;
}
这里,我们不需要使用view,所以直接exit。我们直接在浏览器查看的话,它会直接打印出request这个object的所有内容。
访问http://localhost/helloRecess/printIt,看到以下信息(直接打印在屏幕上的有些乱,这里是查看源代码后的结构):
/helloRecess/printIt Request Object
(
[accepts] => Accepts Object
(
[headers:protected] => Array
(
[ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
[ACCEPT_ENCODING] => gzip,deflate,sdch
[ACCEPT_LANGUAGE] => zh-CN,zh;q=0.8,en;q=0.6
[CACHE_CONTROL] => max-age=0
[CONNECTION] => keep-alive
[COOKIE] => CNZZDATA5619041=cnzz_eid%3D1321855538-1388926353-%26ntime%3D1388926353%26cnzz_a%3D0%26ltime%3D1388926352365; session=GYjQdK9TIghhNg5jeOX40Wj5fswl1qoGNYg0spSAT9qqptZRI4TIZALE2Gz46lKT
[HOST] => www.noarduino.com
[USER_AGENT] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36
) [format:protected] =>
[formats:protected] =>
[formatsTried:protected] => Array
(
) [formatsCurrent:protected] => Array
(
) [languages:protected] =>
[encodings:protected] =>
[charsets:protected] =>
) [format] => html
[headers] => Array
(
[ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
[ACCEPT_ENCODING] => gzip,deflate,sdch
[ACCEPT_LANGUAGE] => zh-CN,zh;q=0.8,en;q=0.6
[CACHE_CONTROL] => max-age=0
[CONNECTION] => keep-alive
[COOKIE] => CNZZDATA5619041=cnzz_eid%3D1321855538-1388926353-%26ntime%3D1388926353%26cnzz_a%3D0%26ltime%3D1388926352365; session=GYjQdK9TIghhNg5jeOX40Wj5fswl1qoGNYg0spSAT9qqptZRI4TIZALE2Gz46lKT
[HOST] => www.noarduino.com
[USER_AGENT] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36
) [resource] => /helloRecess/printIt
[resourceParts] => Array
(
[0] => helloRecess
[1] => printIt
) [method] => GET
[input] =>
[isAjax] =>
[get] => Array
(
) [post] => Array
(
) [put] => Array
(
) [cookies] => Array
(
[CNZZDATA5619041] => cnzz_eid=1321855538-1388926353-&ntime=1388926353&cnzz_a=0<ime=1388926352365
[session] => GYjQdK9TIghhNg5jeOX40Wj5fswl1qoGNYg0spSAT9qqptZRI4TIZALE2Gz46lKT
) [meta] => Meta Object
(
[app] => HelloRecessApplication Object
(
[name] => Hello Recess
[controllersPrefix] => helloRecess.controllers.
[modelsPrefix] => helloRecess.models.
[viewsDir] => /home/qkdemoco/domains/noarduino.com/public_html/apps/helloRecess/views/
[assetUrl] => /apps/helloRecess/public/
[routingPrefix] => helloRecess/
[plugins] => Array
(
) [viewPathFinder:protected] => PathFinder Object
(
[paths:protected] => Array
(
[0] => /home/qkdemoco/domains/noarduino.com/public_html/recess/recess/framework/ui/parts/
[1] => /home/qkdemoco/domains/noarduino.com/public_html/apps/helloRecess/views/
) ) ) [controllerMethod] => printIt
[controllerMethodArguments] => Array
(
) [useAssociativeArguments] => 1
[controller] => HelloRecessHomeController Object
(
[request:protected] => Request Object
*RECURSION*
[headers:protected] =>
[application:protected] => HelloRecessApplication Object
(
[name] => Hello Recess
[controllersPrefix] => helloRecess.controllers.
[modelsPrefix] => helloRecess.models.
[viewsDir] => /home/qkdemoco/domains/noarduino.com/public_html/apps/helloRecess/views/
[assetUrl] => /apps/helloRecess/public/
[routingPrefix] => helloRecess/
[plugins] => Array
(
) [viewPathFinder:protected] => PathFinder Object
(
[paths:protected] => Array
(
[0] => /home/qkdemoco/domains/noarduino.com/public_html/recess/recess/framework/ui/parts/
[1] => /home/qkdemoco/domains/noarduino.com/public_html/apps/helloRecess/views/
) ) ) ) ) [username] =>
[password] =>
)
在这里,一个Request携带了哪些信息一目了然,至于调用的方法:
1)字符串。比如上面的resource项,直接使用$this->request->resource即可。
2)array。比如上面的headers项中Host项,使用$this->request->headers[HOST]即可。get、post、put三个array的取用方法也一样,对应Key=value;可以尝试访问http://localhost/helloRecess/printIt?foo=bar,看看上面的request->get这个array里面有什么变化。
recess----2.Controller里面取用request信息的更多相关文章
- SpringMVC Controller中注入Request成员域和在方法中定义中HttpServletRequest有啥区别
先说结论,在Controller中注入Request是线程安全的. 以下是解释: 我们先来看看这两者有什么不同 在controller注入成员变量request 可以看到注入的是一个代理对象 写在方法 ...
- spring mvc controller中获取request head内容
spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...
- python 抓取request信息,各种cookie,user-agent类的信息,只调试到http可以抓取,https貌似不行。
import pcap # 安装的是pypcap,本博客有安装方法,不过也比较乱,试试吧.import dpktimport socketimport datetime def sniffer(str ...
- ASP.NET Core 如何记录每次请求的Request信息 - sky 胡萝卜星星 - CSDN博客
原文:ASP.NET Core 如何记录每次请求的Request信息 - sky 胡萝卜星星 - CSDN博客 版权声明:本文为starfd原创文章,转载请标明出处. https://blog.csd ...
- 支付宝小程序serverless---获取用户信息(头像)并保存到云数据库
支付宝小程序serverless---获取用户信息(头像)并保存到云数据库 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 我又 ...
- 在springMVC的controller中获取request,response对象的一个方法
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttr ...
- controller 监控Unix性能信息
linux系统需要有RPC(Remote Procedure Call Protocol),远程过程调用协议,通过安装rpc.rstatd程序,启动其服务,就可以给远程机器提供信息,即Lr可以获取到该 ...
- Spring-boot 配置Aop获取controller里的request中的参数以及其返回值
首先在你的Maven的pom文件里加入aop的依赖: <dependency> <groupId>org.springframework.boot</groupId> ...
- 【转】在SpringMVC Controller中注入Request成员域
原文链接:https://www.cnblogs.com/abcwt112/p/7777258.html 原文作者:abcwt112 主题 在工作中遇到1个问题....我们定义了一个Controlle ...
随机推荐
- What is Double Spending & How Does Bitcoin Handle It?
https://coinsutra.com/bitcoin-double-spending/ Bitcoin is gaining rapid popularity and adoption acro ...
- HDU 6373 Pinball
Pinball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- 【BZOJ 3993】【SDOI 2015】序列统计
http://www.lydsy.com/JudgeOnline/problem.php?id=3992 这道题好难啊. 第一眼谁都能看出来是个dp,设\(f(i,j)\)表示转移到第i位时前i位的乘 ...
- BZOJ 3632 外太空旅行(最大团)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3632 [题目大意] 求图中的最大团. [题解] 最大团问题是npc问题,因此可以考虑随 ...
- [转]MySQL与Oracle的语法区别详细对比
Oracle和mysql的一些简单命令对比 1) SQL> select to_char(sysdate,'yyyy-mm-dd') from dual; SQL> select to_c ...
- Manthan, Codefest 16 D. Fibonacci-ish 暴力
D. Fibonacci-ish 题目连接: http://www.codeforces.com/contest/633/problem/D Description Yash has recently ...
- MYSQL数据库迁移总结
Auth: JinDate: 2014-04-03 前端操作,后续测试对外开放不在本文 准备阶段操作阶段后续阶段 需求:phpcms和anquanzuo在一个mysql实例中anquanzuo有一张视 ...
- nor flash 和nand flash 的区别
ROM和RAM指的都是半导体存储器,ROM是Read Only Memory的缩写,RAM是Random Access Memory的缩写.ROM在系统停止供电的时候仍然可以保持数据,而RAM通常都是 ...
- nginx实现简单负载均衡
配置文件 主文件: /etc/nginx/nginx.config 例当前在22.22.22.4服务器 //同时开另外三个服务器 可以在http{} 里添加 include /etc/nginx/si ...
- 客户端Git的常用命令
(1)git clone 服务器用户名@服务器IP:~/Git目录/.git 功能:下载服务器端Git仓库中的文件或目录到本地当前目录. (2)git status 功能:查看Git仓库中的文件状态. ...