分享

使用zend framework 搭建网站(十二)

 sumi2005 2014-03-31

使用zend framework 搭建网站(十二)-- RESTful架构 面向资源开发

作者:冷锋 

ZF系列介绍文章:http://hi.baidu.com/billdkj/archive/tag/zend%20framework

Zend Framework版本:1.1


     最近发现喜欢zend framework框架的朋友越来越多了,这些年我也一直在用zend framework框架开发,算不上很了解,略知一二吧! 继续拾起这个系列文章,再写点东西。tips:其实框架本身并不重要,重要的是思路。

       我超级喜欢面向资源的开发思想,化繁为简,一切回到它本来的样子。


先引用一段RESTful介绍(介绍内容摘录自阮一峰的网络日志,看这里):

名称

       Fielding将他对互联网软件的架构原则,定名为REST,即Representational State Transfer的缩写。我对这个词组的翻译是"表现层状态转化"。

       如果一个架构符合REST原则,就称它为RESTful架构。

       要理解RESTful架构,最好的方法就是去理解Representational State Transfer这个词组到底是什么意思,它的每一个词代表了什么涵义。如果你把这个名称搞懂了,也就不难体会REST是一种什么样的设计。


资源(Resources)

       REST的名称"表现层状态转化"中,省略了主语。"表现层"其实指的是"资源"(Resources)的"表现层"。

       所谓"资源",就是网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的实在。你可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的URI。要获取这个资源,访问它的URI就可以,因此URI就成了每一个资源的地址或独一无二的识别符。

       所谓"上网",就是与互联网上一系列的"资源"互动,调用它的URI。


表现层(Representation)

       "资源"是一种信息实体,它可以有多种外在表现形式。我们把"资源"具体呈现出来的形式,叫做它的"表现层"(Representation)。

       比如,文本可以用txt格式表现,也可以用HTML格式、XML格式、JSON格式表现,甚至可以采用二进制格式;图片可以用JPG格式表现,也可以用PNG格式表现。

       URI只代表资源的实体,不代表它的形式。严格地说,有些网址最后的".html"后缀名是不必要的,因为这个后缀名表示格式,属于"表现层"范畴,而URI应该只代表"资源"的位置。它的具体表现形式,应该在HTTP请求的头信息中用Accept和Content-Type字段指定,这两个字段才是对"表现层"的描述。


状态转化(State Transfer)

       访问一个网站,就代表了客户端和服务器的一个互动过程。在这个过程中,势必涉及到数据和状态的变化。

       互联网通信协议HTTP协议,是一个无状态协议。这意味着,所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生"状态转化"(State Transfer)。而这种转化是建立在表现层之上的,所以就是"表现层状态转化"。

       客户端用到的手段,只能是HTTP协议。具体来说,就是HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源。


综述

综合上面的解释,我们总结一下什么是RESTful架构:

  (1)每一个URI代表一种资源;

  (2)客户端和服务器之间,传递这种资源的某种表现层;

  (3)客户端通过四个HTTP动词,对服务器端资源进行操作,实现"表现层状态转化"。


RESTful 就不介绍了,大家可以参考infoq的系列文章。


Zend 里面可以用Zend_Rest_Controller来实现的


1、上段Zend_Rest_Controller介绍:


Zend_Rest_Controller ?

To help guide development of Controllers for use with Zend_Rest_Route, extend your Controllers from Zend_Rest_ControllerZend_Rest_Controller defines the 5 most-commonly needed operations for RESTful resources in the form of abstract action methods.

  • indexAction() - Should retrieve an index of resources and assign it to view.

  • getAction() - Should retrieve a single resource identified by URI and assign it to view.

  • postAction() - Should accept a new single resource and persist its state.

  • putAction() - Should accept a single resource idenitifed by URI and persist its state.

  • deleteAction() - Should delete a single resource identified by URI.


2、使用

控制器继承Zend_Rest_Controller,实现5个动作,index是获取列表,get是获取id为某某的一个资源,post是新增资源,put是更新id为某某的资源,delete是删除id为某某的资源。


以图书资源为示例,server端:

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
<?php
/**
 * 图书A
 * @author lengfeng  
 * 数据的传递是用JSON格式
 * 可以统一关闭视图助手,我这里每个地方设置一下,有点麻烦,主要是方便大家看。我把资源放在rest模块下了,有点多此一举呀
 */
class Rest_BookController extends Zend_Rest_Controller
{
    /**
     * 图书列表
     */
    public function indexAction()
    {
          $this->_helper->viewRenderer->setNoRender ();
        header ( 'Content-type: application/json' );
          
        //$books是图书列表
        $data array ('rsp' => array ('book' => array ('totalResults' => $total_results'items' => $books ) ) );
        $data = Zend_Json::encode ( $data );
        echo $data;
        return;
    }
    /**
     * 一本书信息
     */
    public function getAction()
    {
          $this->_helper->viewRenderer->setNoRender ();
        header ( 'Content-type: application/json' );
        $id strtolower ( trim ( $this->_getParam ( 'id'"" ) ) );
        $data array ('rsp' => array ('book' => $book_data ) );
        $data_json = Zend_Json::encode ( $data );
        echo $data_json;
        return;
    }
    /**
     * 新增一本书信息
     * 图书信息使用JSON编码后传递过来
     */
    public function postAction()
    {
          $this->_helper->viewRenderer->setNoRender ();
        header ( 'Content-type: application/json' );
        $data_json = trim ( $this->_getParam ( "data""" ) );
        $data = Zend_Json::decode ( $data_json );
          
        /**
         * 新增图书信息,返回结果
         */
        
        $created = local_date ( "Y-m-d H:i:s", gmtime () );
        $data array ('rsp' => array ('book' => array ('created' => $created ,'book_id' => $book_id) ) );
        $data_json = Zend_Json::encode ( $data );
        echo $data_json;
        return;
    }
    /**
     * 更新一本书信息
     */
    public function putAction()
     {
          $this->_helper->viewRenderer->setNoRender ();
        header ( 'Content-type: application/json' );
        $data_json = trim ( $this->_getParam ( "data""" ) );
        $data = Zend_Json::decode ( $data_json );
        $book_id $data ["book_id"];
        unset ( $data ["book_id"] );
        /**
         * 更新图书信息
         */
        //end
          
        $modified = local_date ( "Y-m-d H:i:s", gmtime () );
        $data array ('rsp' => array ('book' => array ('modified' => $modified"book_id" => $book_id ) ) );
        $data_json = Zend_Json::encode ( $data );
        echo $data_json;
        return;
    }
    /**
     * 删除一本书信息
     */
    public function deleteAction()
     {
          $this->_helper->viewRenderer->setNoRender ();
        header ( 'Content-type: application/json' );
        //图书id
        $id strtolower ( trim ( $this->_getParam ( 'id'"" ) ) );
        /**
         * 删除图书信息
         */
                  
        $deleted = local_date ( "Y-m-d H:i:s", gmtime () );
        $data array ('rsp' => array ('book' => array ('deleted' => $deleted'book_id' => $book_id ) ) );
        $data_json = Zend_Json::encode ( $data );
        echo $data_json;
        return;
    }
}

不算是很标准,不过用着还行哈


客户端:


使用面向资源模式开发,一个好处是资源都是无状态的,你可以把每个资源都部署到一台服务器上,甚之可以把一个资源部署到很多台服务器上然后用nginx负载,这样后端的资源就可以支撑起比较大的服务压力了。所以我在客户端封装了一个Rest类,客户端请求后端资源调用这个类即可


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
<?php
/**
 * Rest请求
 * @author 冷锋
 */
class Rest
{
    /**
     * rest配置
     */
    protected $rest_config array ();
    /**
     * 资源类别
     */
    protected $kind "book";
    /**
     * Action
     */
    protected $action "get";
    /**
     * 数据传送方式
     */
    protected $send_mothod "GET";
    /**
     * 模块
     */
    protected $module "rest";
    /**
     * param
     */
    protected $param array ();
    /**
     * client 对象
     */
    public $client = null;
    /**
     * 静态实例
     */
    protected static $_instance = null;
    /**
     * 构造函数
     
     * Method               URI                                 Module_Controller::action
     * GET          /product/ratings/                       Product_RatingsController::indexAction()
     * GET          /product/ratings/:id                    Product_RatingsController::getAction()
     * POST                 /product/ratings                        Product_RatingsController::postAction()
     * PUT          /product/ratings/:id                    Product_RatingsController::putAction()
     * DELETE               /product/ratings/:id                    Product_RatingsController::deleteAction()
     * POST                 /product/ratings/:id?_method=PUT        Product_RatingsController::putAction()
     * POST                 /product/ratings/:id?_method=DELETE             Product_RatingsController::deleteAction()
     */
    public function __construct($kind = null, $action = null, $send_mothod = null, $param = null, $module "rest")
    {
        require "config/rest_config.php";  //我比较懒了,直接就这样设置配置文件了^_^
        $this->rest_config = $rest_config;
        $this->client = new Zend_Http_Client ( null, array ('maxredirects' => 0, 'timeout' => 100, 'useragent' => 'rest_agent' ) );
        $this->setConfig ( $kind$action$send_mothod$param$module );
    }
    /**
     * Singleton instance
     * @return static $_instance
     */
    public static function getInstance()
    {
        if (null === self::$_instance)
        {
            self::$_instance new self ();
        }
                
        return self::$_instance;
    }
    /**
     * 返回Zend_Http_Client对象
     */
    public function getHttp()
    {
        return $this->client;
    }
    /**
     * 设置参数
     * @param string $kind
     * @param string $action
     * @param string $send_mothod
     * @param array $param
     */
    public function setConfig($kind = null, $action = null, $send_mothod = null, $param = null, $module = null)
    {
        if ($kind != null)
        {
            $this->setKind ( $kind );
        }
        if ($action != null)
        {
            $this->setAction ( $action );
        }
        if ($send_mothod != null)
        {
            $this->setSendMethod ( $send_mothod );
        }
        /**
         * 设置参数
         */
        $this->setParam ( $param );
        /**
         * 设置module
         */
        if ($module != null)
        {
            $this->module = $module;
        }
        /**
         * 设置URL
         */
        $this->setUrl ();
        /**
         * Set the next request's method
         */
        $this->setMethod ();
    }
    /**
     * 设置数据类别
     */
    protected function setKind($kind)
    {
        $this->kind = $kind;
    }
    /**
     * 设置方法
     */
    protected function setAction($action)
    {
        $this->action = $action;
    }
    /**
     * 设置数据传送方式
     */
    protected function setSendMethod($send_mothod)
    {
        $this->send_mothod = $send_mothod;
    }
    /**
     * 设置参数
     */
    protected function setParam($param)
    {
        if ($this->send_mothod == 'POST')
        {
            if ($this->action == "put")
            {
                $param ["_method"] = "PUT";
            }
            if ($this->action == "delete")
            {
                $param ["_method"] = "DELETE";
            }
            $this->client->resetParameters ()->setParameterPost ( $param );
        }
        else
        {
            $this->client->resetParameters ()->setParameterGet ( $param );
        }
            
    }
    /**
     * 设置URL
     */
    protected function setUrl()
    {
        $url "";
        $url $this->rest_config [$this->kind] . "/" $this->module . "/" $this->kind . "/";
        $url .= $this->action;
        $this->client->setUri ( $url );
    }
    /**
     * Set the next request's method
     */
    protected function setMethod()
    {
        if ($this->send_mothod == "GET")
        {
            switch ($this->action)
            {
                case "index" :
                case "get" :
                    $this->client->setMethod ( "GET" );
                    break;
                case "put" :
                    $this->client->setMethod ( "PUT" );
                    break;
                case "delete" :
                    $this->client->setMethod ( "DELETE" );
                    break;
            }
        }
        if ($this->send_mothod == "POST")
        {
            $this->client->setMethod ( "POST" );
        }
    }
    public function getData()
    {
        $response $this->client->request ();
        /**
         * @see Zend_Json
         */
        require_once 'Zend/Json.php';
        return Zend_Json::decode ( $response->getBody () );
    }
}


客户端配置:

1
2
3
4
5
6
7
<?php
/**
 * rest 服务配置文件
 */
$rest_config array(
    "book"  => "http://192.168.1.11:88",
);

客户端调用:

1
2
3
4
5
6
7
<?php
/**
 * 读取一本书的信息
 */
$Rest new Rest ( "book""get""GET"array ("id" => 11 ), "rest" );
$result $Rest->getData ();
$book_info $result["rsp"] ["book"];

ok!


使用RESTful还可以设置缓存啥的,可以增加安全设置,比如对请求客户端的IP限制,根据IP的不同能够访问的服务也不同,不能做delete等危险操作等



参考资料:

理解RESTful架构    http://www./blog/2011/09/restful.html

rest百科   http://baike.baidu.com/view/1077487.htm

拙议REST及其在PHP中的现状  http://hi.baidu.com/thinkinginlamp/item/bf28844e1f5eef0e6dc2f0b8

深入浅出REST  http://www./cn/articles/rest-introduction

Zend_Rest_Controller介绍   http://framework./manual/1.9/en/zend.controller.router.html


http://www./cn/articles/rest-architecure

http://www./cn/articles/rest-anti-patterns

http://www./cn/articles/webber-rest-workflow

http://www./cn/articles/tilkov-rest-doubts

http://www./cn/articles/designing-restful-http-apps-roth

http://www./cn/articles/roa-rest-of-rest

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多