php - Zend framework Restful webserive logic usage -


i building application using zend framework.

the question is, how use same controller logic of zend_rest_controller within non-rest app.

for instance, let's assume twitter written zend framework. use zend_rest_controller , route api. however, use website (which use same api logic)? write entire new application fire rest request? isn't overload.

[edit]
if web app calls api through http_client class data, makes request server (it causes performance degrade , slow down response). don't want make request , want use same business logic in api.

thanks,
venu

new answer:

i have come pattern seems work well. solves of concerns: here scaled down version of came with:

first need our own controller. conroller have service proxies action request service, if not defined:

abstract class app_rest_controller extends zend_controller_action {     /**       * @var app_rest_service_abstract      */     protected $_service;      public function __call($methodname, $args)     {         if ('action' == substr($methodname, -6)) {             $action = substr($methodname, 0, strlen($methodname) - 6);             return $this->_service()->$action();         }          return parent::__call($methodname, $args);     } } 

now time service. extend action helper abstract that:

  1. we have direct access request object
  2. we can call service controller

this act go between the application , actual storage of data.

abstract class app_rest_service_abstract extends zend_controller_action_helper_abstract {     /*      * @var app_rest_storage_interface      */     protected $_storage;     public function __call($methodname, $args)     {         if (!method_exists($this->getstorage(), $methodname)) {             throw new app_rest_service_exception(sprintf('the storage not have method "%s"', $methodname));         }          switch ($methodname) {             case 'get':             case 'put':             case 'delete':                 //if id param isnot set, throw exception                 if (false === ($id = $this->getrequest()->getparam('id', false))) {                     throw new  app_rest_service_exception(sprintf('method "%s" expects id param, none provided', $methodname));                 }                 $iterator = $this->getstorage()->$methodname($id, $this->getrequest()->getparams());                 break;             case 'index':             case 'post':             default:                 //if index, post or not tradition restful request, function must expect first , argument array                 $iterator =  $this->getstorage()->$methodname($this->getrequest()->getparams());                 break;         }           return $this->_getresult($iterator);     }      protected function _getresult($iterator) { /*        * write own, in case make paginator ,        *  either return or send data via json helper        *        /* } 

now interface. actual work of storing, modifying , returning data. beauty of using interface can implement no matter use model layer. created abstract storage has zend_form (for validation) , zend_db_table actual data. implement on object.

interface app_rest_storage_interface extends zend_validate_interface {     public function index(array $params = null);      public function get($id, array $params = null);      public function post(array $params);      public function put($id, array $params);      public function delete($id, array $params);  } 

now operation anywhere within site. suppose have "customers" service. inside controller simple as

$customer = $this->_helper->helper->customers->get(1); 

anywhere else (say view helper example):

zend_controller_action_helperbroker::getstatichelper('customers')->get(1) 

i hope helps. working me.


Comments