2009-03-16 8 views
0

私のWebアプリケーション用のAPIはどのようにして作成できますか?私はZend_Restを調べていますが、その背後にあるアイデアは把握できません。Zend Framework +私のアプリケーションのAPI

基本的に、私は、この例のようなものしたいと思う:

$technorati = new Zend_Rest_Client('http://api.technorati.com/bloginfo'); 
$technorati->key($key); 
$technorati->url('http://pixelated-dreams.com'); 
$result = $technorati->get(); 
echo $result->firstname() .' '. $result->lastname(); 

をしかし、私は、Zend Frameworkを使用して、それを実装する方法に完全に失われています。 Zend_Rest_Serverを使用する必要がありますか?どのようにこれを行うにはどのような方法の指導は、大いに感謝します...

答えて

2

​​の問題は何ですか?

/** 
* Say Hello 
* 
* @param string $who 
* @param string $when 
* @return string 
*/ 
function sayHello($who, $when) 
{ 
    return "Hello $who, Good $when"; 
} 

$server = new Zend_Rest_Server(); 
$server->addFunction('sayHello'); 
$server->handle(); 

あなたは特に問題はありませんか?Zend_Rest_Serverを使用する方法に関する絶対的な最小例がありますか?

EDIT: これはあなたがWebサービスのために必要としない多くの機能と一緒にEXTREMのオーバーヘッドを導入することを、私は思うMVCの統合に関するご質問について。それにもかかわらず、大きなオーバーヘッドがなくても、MVCにサービスを統合することは可能です。

class Hello 
{ 
    /** 
    * Say Hello 
    * 
    * @param string $who 
    * @param string $when 
    * @return string 
    */ 
    function sayHello($who, $when) 
    { 
     return "Hello $who, Good $when"; 
    } 
} 

class ApiController extends Zend_Controller_Action 
{ 
    /** 
    * @var Zend_Rest_Server 
    */ 
    protected $_server; 

    public function init() 
    { 
     // disable view rendering 
     $this->_helper->viewRenderer->setNeverRender(true); 
     // disable layout (if you use layouts) 
     $this->_helper->layout->disableLayout(); 

     $this->_server = new Zend_Rest_Server(); 
     $this->_server->setClass('Hello'); 
    } 

    public function __call($method, $args) 
    { 
     $params = $this->_getAllParams(); 
     $params['method'] = $method; 
     $this->_server->handle($params); 
    } 
} 

/* 
* This should allow you to call http://www.example.com/api/sayHello/who/MyName/when/morning 
* By defining custom routes you're able to control the urls. 
*/ 

コードがテストされていないと確実に特にエラー処理に関するいくつかの調整を必要とするが、それは簡単なMVCのサービスの例として使用できることに注意してください。

+0

MVCでの使用方法の良い例が見つかりませんでした。あなたはコントローラの中でそれをする方法を私に見せてもらえますか? – wenbert

+0

ありがとうございます。私は十分な評判を持っていれば、私はupvoteするだろう:)答えを本当に感謝:D – wenbert

+0

JUstは明確にする:あなたはしていない他のZend Frameworkコンポーネントを使用するだけでMVCを使用する必要があります。このようにして、Zend Frameworkは他のPHPフレームワークほど厳密ではありません。これは単なるコンポーネント間の依存関係ではありません。 –

関連する問題