Programmer's Reference Guide

Zend_Rest_Client(日本語)

Zend_Rest_Server(日本語)

導入

Zend_Rest_Server は、完全に機能する REST サーバを作成するためのものです。

REST サーバの使用法

例1 基本的な Zend_Rest_Server の使用法 - クラス

  1. $server = new Zend_Rest_Server();
  2. $server->setClass('My_Service_Class');
  3. $server->handle();

例2 基本的な Zend_Rest_Server の使用法 - 関数

  1. /**
  2. * Say Hello
  3. *
  4. * @param string $who
  5. * @param string $when
  6. * @return string
  7. */
  8. function sayHello($who, $when)
  9. {
  10.     return "Hello $who, Good $when";
  11. }
  12.  
  13. $server = new Zend_Rest_Server();
  14. $server->addFunction('sayHello');
  15. $server->handle();

Zend_Rest_Server サービスのコール

Zend_Rest_Server サービスをコールするには、 GET/POST 時の引数 method にそのメソッド名を指定しなければなりません。 その後に、任意の数の引数を続けることができます。これは、引数の名前 (たとえば "who") を指定するか、あるいは引数の位置を表す数値 (たとえば "arg1") を指定します。

注意: 数値インデックス
数値で指定する引数のインデックスは、1 から始まります。

上の例の sayHello をコールするには、次のようにします。

?method=sayHello&who=Davey&when=Day

あるいは、このようにもできます。

?method=sayHello&arg1=Davey&arg2=Day

独自のステータスの送信

値を返す際に独自のステータスを返すには、 キー status を含む配列を返します。

例3 独自のステータスを返す

  1. /**
  2. * Say Hello
  3. *
  4. * @param string $who
  5. * @param string $when
  6. * @return array
  7. */
  8. function sayHello($who, $when)
  9. {
  10.     return array('msg' => "An Error Occurred", 'status' => false);
  11. }
  12.  
  13. $server = new Zend_Rest_Server();
  14. $server->addFunction('sayHello');
  15. $server->handle();

独自の XML レスポンスを返す

独自の XML を返したい場合は、 DOMDocumentDOMElement あるいは SimpleXMLElement オブジェクトを返します。

例4 独自の XML を返す

  1. /**
  2. * Say Hello
  3. *
  4. * @param string $who
  5. * @param string $when
  6. * @return SimpleXMLElement
  7. */
  8. function sayHello($who, $when)
  9. {
  10.     $xml ='<?xml version="1.0" encoding="ISO-8859-1"?>
  11. <mysite>
  12.     <value>Hey $who! Hope you\'re having a good $when</value>
  13.     <code>200</code>
  14. </mysite>';
  15.  
  16.     $xml = simplexml_load_string($xml);
  17.     return $xml;
  18. }
  19.  
  20. $server = new Zend_Rest_Server();
  21. $server->addFunction('sayHello');
  22.  
  23. $server->handle();

サービスからのレスポンスは、変更なしにクライアントに返されます。


Zend_Rest_Client(日本語)

Comments

@Andrew: Agreed. I wish this was more informative.
How is requesting a "sayHello" method RESTful? "sayHello" is not a resource, so I don't understand how this is RESTful at all. Calling a custom method is a SOAP paradigm.

I also don't understand what this "Zend_Rest_Server" is supposed to do and why it's useful. "Zend_Rest_Server is intended as a fully-featured REST server." is not enough to explain what this class is intended to be used for. A REST server should be able to:

1. receive and parse a request
2. retrieve, create, update, or delete the requested resource
3. then respond with a representation of the resource in the appropriate format (XML or JSON)

...and that's it! Nothing more. Nothing less.

There is nothing in this documentation that makes me think that Zend_Rest_Server does any of that. How are we even supposed to use this?
Thanks again Zend for providing useful documentation.
I doubt if Zend really have to provide the server and client APIs, coz they are not adding any value to RESTfulness of a Web Service

I would expect these server and client classes to provide some common framework method on handling requests, responses, errors, GET, POST, PUT, DELETE methods


Like a POST and PUT methods should always return the URI of the resource just got created or updated.

I don't see any such thing in these classes, hence better these should be removed, else programmers will wrongly use these, which is more of SOAP or RPC rest invocation classes.
to use put you have to add this

resources.frontController.plugins.putHandler = "Zend_Controller_Plugin_PutHandler"

it's basically a crappy little helper that checks to see if there's a put request and if so then it puts the variables you're sending along with it into the params. idk why the params aren't included by default when trying to see your vars passed in the putAction
You're right, this class's documentation is shocking. But Zend does come with a good Rest solution, it's just not here. It's hidden in the Controller docs:

http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.rest

Zend_Rest_Server, as pointed out in this comments, isn't really that useful and is probably on its way out.

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual