2012-03-04 97 views
0

我想弄清楚下面发生了什么:Zend框架Krixton_JsonRpc_Client和呼叫方法

public function serviceAction(){ 
$config = Zend_Registry::get('config'); 

$client = new Zend_Http_Client(); 
$client->setAuth($config['api']['username'],$config['api']['password']); 

$service = new Krixton_JsonRpc_Client($config['api']['endpoint'], $client); 

switch($this->_getParam('task')) 
{ 
    case 'test': 
     if(!this->getParam('newsletter_id')){ 
      $this->_helper->json(array('sent'=>false,'error'=>'Newsletter ID is invalid, must be numeric')); 
      return; 
     } 

     $request = $service->call('newsletter.send', array($this->_getParam('newsletter_id'),false)); 
     $this->_helper->json($request->result)); 
    break; 

} 

} 

我试图找出如何做

`Zend_Registry::get('config')`, $client->setAuth and $service->call` 

的作品?

我明白_getParam('task')是一种获取变量或变量但不确定其他变量的方法。我查看了一些Zend文档,但是如果有人能帮助我,那将不胜感激!

回答

2

这里发生了两件事,第一件是Zend_Registryget()允许您通过Zend_Registry::set('key', $value)获得先前在注册表中注册的值。通常,'config'是您的应用程序配置,它是application.ini文件。

基本上,你会使用这种方法,引导注册配置

protected function _initConfig() 
{ 
    $config = new Zend_Config($this->getOptions()); 
    Zend_Registry::set('config', $config); 
    return $config; 
} 

第二的有无非Zend_Http_Client方法别的。 setAuth()用于设置基本的HTTP认证,call()是对象Krixton_JsonRpc_Client的内部方法。

如果你想深入了解这些方法是如何工作的,你应该首先阅读man(特别是Zend_registryZend_Http_Client页),然后仔细阅读源代码。

2

Zend_Registry::get('config')(“配置”在这种情况下,一个数组的名字)是回顾,是保存到registry数据,大概在Bootstrap.php使application.ini(配置文件)中的信息随处可见。

的bootstrap.php中可能caontains类似于:

protected function _initRegistry() { 

     //make application.ini configuration available in registry 
     $config = new Zend_Config($this->getOptions()); 
     Zend_Registry::set('config', $config); 
    } 

$client->setAuth是简单地提供用户凭据Zend_Http_Client()HTTP LINK已存储的配置文件中,并通过$配置阵列访问。

$service->call我很确定这样做是因为我不熟悉正在使用的类(可能是自定义)。它看起来像一个通讯的请求是基于'id'。