0
Zend Framework“at your service”       Michelangelo van Dam Dutch PHP Conference 2011 Amsterdam (NL)
Why a service ?•- opening up existing API   internal applications - external applications - additional functionality• supp...
Important components• - Zend_Soap      Zend_Soap_Server    - Zend_Soap_Client    - Zend_Soap_Wsdl•    Zend_Rest    - Zend_...
Example service•- Time registration     list time sheets -   add a new task -   edit an existing task -   delete an existi...
MVC approach•- time module     controllers (for displaying listing and forms) -   actions (for listing, adding, editing an...
Common behavior• all “logic” is put in the controller• actions = API interface•- downside     not flexible towards other cl...
Time Module              Time_IndexController indexAction                editAction- lists registered tasks   - displays a...
API Design•- moving logic    out the controller - in an API class• structures the application• is better testable• is bett...
Time API                My_Api_Timesheet listTasks                  registerNewTask- lists registered tasks   - for adding...
Simple ?
API output channels Web    Internal    SOAP XML    API        XMLRPC JSON     …         REST
Timesheet API
DocBlocks are important!•- DocBlocks are very important !     provide quality API documentation -   can be used as referen...
My_Api_Timesheet   <?phpclass My_Api_Timesheet{}
listTasks/**  * List all tasks for a given user  *  * @param int $user  * @return array  */public function listTasks($user...
registerNewTask/** * Register a new task * * @param int $user The ID of the user * @param int $customer The ID of the cust...
registerNewTaskpublic function registerNewTask(        $user, $customer, $task, $date, $start, $end, $description){	 $time...
editExistingTask  /*** Modify an existing task** @param int $id The ID of an existing Task* @param int $user The ID of the...
editExistingTask    public function editExistingTask(                   $id, $user, $customer, $task, $date, $start, $end,...
deleteExistingTask     /**  * Modify an existing task  *  * @param int $id The ID of an existing Task  * @param int $user ...
_validate     /**  * Private validation method  *  * @param Time_Model_Timesheet $timesheet  * @return bool TRUE if valida...
Zend_XmlRpc
XmlRpc Server  <?phpclass Time_XmlRpcController extends Zend_Controller_Action{    public function indexAction()    {     ...
Zend_Soap
SOAP Server  <?phpclass Time_SoapController extends Zend_Controller_Action{    public function indexAction()    {    	$thi...
SOAP Client      <?phpclass Time_SoapClientController extends Zend_Controller_Action{	     protected $_client;		     publi...
Zend_Rest
REST Server  <?phpclass Time_RestController extends Zend_Controller_Action{    public function indexAction()    {    	$thi...
REST Client    <?phpclass Time_RestClientController extends Zend_Controller_Action{	 protected $_client;	    public functi...
Patterns ???
Conclusionmoving functionality out the controller          into a library API                   =              saves time1...
You can help !•- find a bug ?    test it - report it - send a patch/fix• need a non-existing component - submit proposal• li...
ZF Bug hunt days   Zend Framework Bughuntdays     every 3rd Thursday and Friday of the month          http://framework.zen...
ZF issue tracker
phpbenelux.eu PHP BENELUX
daycamp4developers.comProject Management    Late August - Early September
Thank you !  Slides on SlideshareGive feedback on Joind.in   http://joind.in/3473
Upcoming SlideShare
Loading in...5
×

Zend framework service

2,266

Published on

Us a single API layer and use Zend Framework to provide all kinds of services (xml-rpc, json, soap, csv, …)

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,266
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
43
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Zend framework service"

    1. 1. Zend Framework“at your service” Michelangelo van Dam Dutch PHP Conference 2011 Amsterdam (NL)
    2. 2. Why a service ?•- opening up existing API internal applications - external applications - additional functionality• support to more devices• portability and flexibility
    3. 3. Important components• - Zend_Soap Zend_Soap_Server - Zend_Soap_Client - Zend_Soap_Wsdl• Zend_Rest - Zend_Rest_Server - Zend_Rest_Client• Zend_XmlRpc - Zend_XmlRpc_Server - Zend_XmlRpc_Client - Zend_XmlRpc_Request - Zend_XmlRpc_Response
    4. 4. Example service•- Time registration list time sheets - add a new task - edit an existing task - delete an existing task
    5. 5. MVC approach•- time module controllers (for displaying listing and forms) - actions (for listing, adding, editing and deleting) - models (for access to database) - forms (for filtering, validation and rendering forms)
    6. 6. Common behavior• all “logic” is put in the controller• actions = API interface•- downside not flexible towards other clients - difficult to provide maintenance - not reusable design
    7. 7. Time Module Time_IndexController indexAction editAction- lists registered tasks - displays a form- links to the form - for adding task - for adding task - for editing task - for editing task - for deleting task registerAction deleteAction- processes form data - deletes task - validating data - storing data in db
    8. 8. API Design•- moving logic out the controller - in an API class• structures the application• is better testable• is better maintainable
    9. 9. Time API My_Api_Timesheet listTasks registerNewTask- lists registered tasks - for adding task editExistingTask deleteExistingTask- for modifying task - deletes task
    10. 10. Simple ?
    11. 11. API output channels Web Internal SOAP XML API XMLRPC JSON … REST
    12. 12. Timesheet API
    13. 13. DocBlocks are important!•- DocBlocks are very important ! provide quality API documentation - can be used as reference for the server !!!
    14. 14. My_Api_Timesheet <?phpclass My_Api_Timesheet{}
    15. 15. listTasks/** * List all tasks for a given user * * @param int $user * @return array */public function listTasks($user){ $array = array (); $timesheet = new Time_Model_Timesheet(); if (null !== ($result = $timesheet->fetchAll(array ( user_id = ? => $user, ), array (date DESC, start_time ASC)))) { foreach ($result as $entry) { $array[] = $entry->toArray(); } } return $array;}
    16. 16. registerNewTask/** * Register a new task * * @param int $user The ID of the user * @param int $customer The ID of the customer * @param int $task The ID of the task * @param string $date A date formatted as YYYY-mm-dd * @param string $start The starting time as HH:mm:ss * @param string $end The ending time as HH:mm:ss * @param string $description A short description * @return bool TRUE if registration succeeded * @throws My_Api_Timesheet_Exception */
    17. 17. registerNewTaskpublic function registerNewTask( $user, $customer, $task, $date, $start, $end, $description){ $timesheet = new Time_Model_Timesheet(); $timesheet->setUserId($user) ->setCustomerId($customer) ->setTaskId($task) ->setDate($date) ->setStartTime($start) ->setEndTime($end) ->setDescription($description); $validator = $this->_validate($timesheet); if (false === $validator) { require_once My/Api/Timesheet/Exception.php; throw new My_Api_Timesheet_Exception(Invalid data provided); } $timesheet->save(); return true;}
    18. 18. editExistingTask /*** Modify an existing task** @param int $id The ID of an existing Task* @param int $user The ID of the user* @param int $customer The ID of the customer* @param int $task The ID of the task* @param string $date A date formatted as YYYY-mm-dd* @param string $start The starting time as HH:mm:ss* @param string $end The ending time as HH:mm:ss* @param string $description A short description* @return bool TRUE if registration succeeded* @throws My_Api_Timesheet_Exception*/
    19. 19. editExistingTask public function editExistingTask( $id, $user, $customer, $task, $date, $start, $end, $description){ $timesheet = new Time_Model_Timesheet(); $timesheet->setId($id) ->setUserId($user) ->setCustomerId($customer) ->setTaskId($task) ->setDate($date) ->setStartTime($start) ->setEndTime($end) ->setDescription($description); $validator = $this->_validate($timesheet); if (false === $validator) { require_once My/Api/Timesheet/Exception.php; throw new My_Api_Timesheet_Exception(Invalid data provided); } $timesheet->save(); return true;}
    20. 20. deleteExistingTask /** * Modify an existing task * * @param int $id The ID of an existing Task * @param int $user The ID of the user * @return bool TRUE if registration succeeded * @throws My_Api_Timesheet_Exception */public function deleteExistingTask($id, $user){ $timesheet = new Time_Model_Timesheet(); $timesheet->setId($id) ->setUserId($user); $validator = $this->_validate($timesheet); if (false === $validator) { require_once My/Api/Timesheet/Exception.php; throw new My_Api_Timesheet_Exception(Invalid data provided); } $timesheet->delete(array ( id = ? => $timesheet->getId(), user_id = ? => $timesheet->getUserId(), )); return true;}
    21. 21. _validate /** * Private validation method * * @param Time_Model_Timesheet $timesheet * @return bool TRUE if validated, FALSE if invalid */private function _validate(Time_Model_Timesheet $timesheet){ $result = true; $validator = new Time_Form_Register(); $customer = new Time_Model_Customer(); $task = new Time_Model_Task(); $validator->getElement(customer_id)->setMultiOptions($customer->toSelect()); $validator->getElement(task_id)->setMultiOptions($task->toSelect()); if (!$validator->isValid($timesheet->toArray())) { $result = false; } return $result;}
    22. 22. Zend_XmlRpc
    23. 23. XmlRpc Server <?phpclass Time_XmlRpcController extends Zend_Controller_Action{ public function indexAction() { $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); require_once My/Api/Timesheet.php; $server = new Zend_XmlRpc_Server(); $server->setClass(My_Api_Timesheet); echo $server->handle(); }}
    24. 24. Zend_Soap
    25. 25. SOAP Server <?phpclass Time_SoapController extends Zend_Controller_Action{ public function indexAction() { $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); $options = array (); $server = new Zend_Soap_AutoDiscover(); $server->setClass(My_Api_Timesheet); $server->handle(); }}
    26. 26. SOAP Client <?phpclass Time_SoapClientController extends Zend_Controller_Action{ protected $_client; public function init() { $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); $this->_client = new Zend_Soap_Client( http://www.demo.local/Time/soap?wsdl, array ()); } public function indexAction() { Zend_Debug::dump($this->_client->listTasks(2)); } public function addAction() { Zend_Debug::dump($this->_client->registerNewTask( 2, 2, 3, 2010-03-30, 18:00:00, 23:30:00,Setting up SOAP)); } public function editAction() { Zend_Debug::dump($this->_client->editExistingTask( 7, 2, 2, 3, 2010-03-30, 18:00:00, 23:30:00,Testing SOAP)); } public function deleteAction() { Zend_Debug::dump($this->_client->deleteExistingTask(7, 2)); }}
    27. 27. Zend_Rest
    28. 28. REST Server <?phpclass Time_RestController extends Zend_Controller_Action{ public function indexAction() { $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); $server = new Zend_Rest_Server(); $server->setClass(My_Api_Timesheet); $server->handle(); }}
    29. 29. REST Client <?phpclass Time_RestClientController extends Zend_Controller_Action{ protected $_client; public function init() { $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); $this->_client = new Zend_Rest_Client(http://www.demo.local/Time/rest); } public function indexAction() { $this->_client->listTasks(2); Zend_Debug::dump($this->_client->get()); }}
    30. 30. Patterns ???
    31. 31. Conclusionmoving functionality out the controller into a library API = saves time1 api = XmlRpc + SOAP + REST + …
    32. 32. You can help !•- find a bug ? test it - report it - send a patch/fix• need a non-existing component - submit proposal• like Zend Framework - blog about it - talk about it
    33. 33. ZF Bug hunt days Zend Framework Bughuntdays every 3rd Thursday and Friday of the month http://framework.zend.com/issues IRC (irc.freenode.net) #zftalk.dev prizes:Free subscription for 1 year on php|Architect magazine Zend Framework t-shirt recognition and appreciation of the community
    34. 34. ZF issue tracker
    35. 35. phpbenelux.eu PHP BENELUX
    36. 36. daycamp4developers.comProject Management Late August - Early September
    37. 37. Thank you ! Slides on SlideshareGive feedback on Joind.in http://joind.in/3473
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×