Create RESTful Applications Using The Zend Framework(使用Zend Framework创建一个Restful应用)

Create RESTful Applications Using The Zend Framework
使用Zend Framework创建一个Restful应用

Submitted by sudheer on Wed, 10/14/2009 - 22:40

[原文]:
The Zend Framework 1.9 release added a new feature - Zend_Rest_Controller. Zend_Rest_Controller and Zend_Rest_Route classes go hand in hand. In the previous versions of the Zend Framework, we have had the Zend_Rest_Server component. We still have. Since Zend_Rest_Server provides an RPC like component violating the REST architectural constraint, it is likely to be deprecated in the future versions of the Zend Framework.
[译文]:
Zend Framework 1.9 release 添加了一个新的特性-Zend_Rest_Controller.Zend_Rest_Controller和Zend_Rest_Route有着密切的关系.在Zend Framework先前的版本中存在的Zend_Rest_Server组建仍然存在.Zend_Rest_Server提供一个RPC(类似...),很有可能会在以后的版本中被抑制.
[原文]:
In this article let us explore how to make use of Zend_Rest_Route and Zend_Rest_Controller to build a RESTful server application. Zend_Rest_Route routes the request to the appropriate module, controller and action depending on the HTTP request method and URI.
[译文]:
在以下文章中让我们看看如何使用Zend_Rest_Route跟Zend_Rest_Controller去建立一个RESTful服务端应用.Zend_Rest_Route路由一个请求到一个专有的module,controller和action依赖http请求方法和url.
[原文]:
Let's start coding. We build the RESTful application based on the QuickStart project.
[译文]:
接下来编码.建立一个基于QuickStart工程的RESTful应用.

[原文]:Adding the Zend_Rest_Route in the bootstrap
[译文]:在引导程序中添加Zend_Rest_Route

[原文]:
You can choose to enable Zend_Rest_Route for the entire application or for specific set of modules. In this example we enable the rest route for the entire application.
[译文]:
你可以为整个应用程序或者特殊设定的module选择可用的Zend_Rest_Route.在下面这个例子中,我们为整个应用程序设定了可用的rest路由.

Bootstrap the front controller resource and add the rest route.
引导前端控制器并且添加rest路由.

<?php
protected function _initRestRoute()
{
        $this->bootstrap('frontController');
        $frontController = Zend_Controller_Front::getInstance();
        $restRoute = new Zend_Rest_Route($frontController);
        $frontController->getRouter()->addRoute('default', $restRoute);
}
?>


Creating the Zend_Rest_Controller
创建Zend_Rest_Controller

 

Create the file application/controllers/ArticleController.php. We extend Zend_Rest_Controller instead of Zend_Controller_Action. In our controller we are going to have five actions.
创建application/controllers/ArticleController.php文件.扩展Zend_Rest_Controller代替Zend_Controller_Action.在我们创建的controller里面我们将会有五个action.

1.indexAction - return all articles 返回所有文章
2.getAction - return a particular article 返回一特定的文章
3.postAction - create a new article 创建一新文章
4.putAction - update a particular article 更新一特定文章
5.deleteAction - delete a particular article 删除一特定文章

These actions are defined as abstract methods in the Zend_Rest_Controller class.

The skeletal controller looks like:

<?php
class ArticleController extends Zend_Rest_Controller
{
    public function init()
    {
        $this->_helper->viewRenderer->setNoRender(true);
    }
    public function indexAction()
    {
    }
    public function getAction()
    {
    }
    
    public function postAction()
    {
    }
    
    public function putAction()
    {
    }
    
    public function deleteAction()
    {
    }
}
?>


For the purpose of brevity, I have disabled the view for this controller in the init() hook.

 

To test the routing of the requests, let's append sample messages to the response object in each action.

<?php
 public function indexAction()
    {
         $this->getResponse()
            ->appendBody("From indexAction() returning all articles");
    }
    public function getAction()
    {
        $this->getResponse()
            ->appendBody("From getAction() returning the requested article");
    }
    
    public function postAction()
    {
        $this->getResponse()
            ->appendBody("From postAction() creating the requested article");
    }
    
    public function putAction()
    {
        $this->getResponse()
            ->appendBody("From putAction() updating the requested article");
    }
    
    public function deleteAction()
    {
        $this->getResponse()
            ->appendBody("From deleteAction() deleting the requested article");
    }
?>


Testing the RESTful server using curl

 

On my computer I have installed this application for the domain zfrest.example.com. We use the curl command to test our RESTful server.

Testing the indexAction() : The URI http://zfrest.example.com/article represents the article resource. All articles are returned for this request.

$ curl http://zfrest.example.com/article


I get the following output:

From indexAction() returning all articles


Testing the getAction() : The URI http://zfrest.example.com/article/1 represents the resource - article 1.

$ curl http://zfrest.example.com/article/1


I get the following output:

From getAction() returning the requested article


Testing the postAction() : we make an HTTP POST request to http://zfrest.example.com.

$ curl -d "article=myarticle" http://zfrest.example.com/article/


I get the following output:

From postAction() creating the requested article


Testing the putAction() : we request the article 1 to be updated by making HTTP PUT request to http://zfrest.example.com/article/1

$ curl -d "article=updatedarticle" -X PUT http://zfrest.example.com/article/1


I get the following output:

From putAction() updating the requested article


Testing the deleteAction() : we send an HTTP DELETE request to http://zfrest.example.com/article/1

curl -X DELETE http://zfrest.example.com/article/1


I get the following output:

From deleteAction() deleting the requested article


Summarizing the exercise

 

Zend Framework allows you to build RESTful server applications using the Zend_Rest_Controller component. The curl command is a very useful tool to test RESTful servers. If you don't have the curl command on your computer, you can write a PHP script and make use of the curl extension provided by PHP. In the upcoming posts of this series, I will discuss managing API keys from your RESTful application, returning appropriate HTTP response codes, reading the body from PUT and DELETE requests and more.

Are you going to a build RESTful server using the Zend_Rest_Controller component? Tell me about your experiences.

About the author
Sudheer is an entrepreneur and software developer. Get more from Sudheer on Twitter.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值