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 ...
随机推荐
- Linux的重定向与管道
(1).输出重定向 定义:将命令的标准输出结果保存到指定的文件中,而不是直接显示在显示器上. 输出重定向使用>和>>操作符. 语法:cmd > filename,表示将标准输出 ...
- CentOS中Ctrl+Z、Ctrl+C、Ctrl+D的区别
Ctrl+C和Ctrl+Z都是中断命令,但作用不同. Ctrl+C是发送SIGINT信号,终止一个进程. Ctrl+Z是发送SIGSTOP信号,挂起一个进程,将作业放置到后台(暂停状态).与此同时,可 ...
- 如何使用Web字体?
如何使用Web字体 嵌入Web字体的关键是@font-face规则,通过它可以指定浏览器下载web字体的地址,以及如何在样式表中引用该字体 @font-face { font-family: Voll ...
- 设计模式-模板方法模式(the Template Method Pattern)
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 模板方法模式简介 这是一个被用的相当广泛的一种设计模式,变体也特别多,他建立一个抽象类定义一个算 ...
- 【LA 3641】 Leonardo's Notebook (置换群)
[题意] 给出26个大写字母组成 字符串B问是否存在一个置换A使得A^2 = B [分析] 置换前面已经说了,做了这题之后有了更深的了解. 再说说置换群. 首先是群. 置换群的元素是置换,运算时是 ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
- 【高斯消元】CDOJ1784 曜酱的线性代数课堂(二)
高斯消元求矩阵秩板子. #include<cstdio> #include<cmath> #include<algorithm> #include<cstri ...
- 20162312Java结对编程之挑战出题
需求分析 实现去重出题,并以命令行参数形式指定题目要求. 设计思路 具体的思路: 思路一: 原本我和春旺商量通过集合中的元素的不重复性进行去重.但是运算符多也导致重复的数字多,去重的数量也大大增多越到 ...
- POJ 3437 Tree Grafting
题意:给出一个深度优先遍历树的up down顺序,求这棵树以及这棵树变为”左子右兄”树的高度 思路:直接dfs,x代表树1的高度,y代表树2的高度 #include<cstdio> #in ...
- C语言读书笔记
1.c语言中一共有32个关键字,分别是: auto.int.double.long.char.short.float.unsigned.signed.sizeof.extern. static.got ...