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里面有什么变化。