Friday, March 7, 2014

WebService ZF2 (RESTful)

RESTful Services made easy with ZF2.

Thera are many ways (technologies) to implement a web-services.

  • Representation State Transfer or REST , that it has been preferred for web services used in web applications. 
  • SOAP web service, also know as WS-* Stack, is also popular alternative. 
But REST has been more popular over web developers.

What is REST:

According to Abeysinghe "

REST is a software architecture style that can be followed while designing software systems. REST is an ideal design style to be followed for web services based software applications."
The key REST principles in brief: 
  • Provide every resource with a unique ID, for example, a URI Link resources with each other, establishing relationships among resources 
  • Use standard methods (HTTP, media types, XML) 
  • Resources can have multiple representations that reflect different application states 
  • The communication should be stateless using the HTTP
Rob Allen said that principal Constraints are:

  • Client/Server
  • Stateless
    •   No client context stored between requests. This means no sessions.
  • Cacheable
    • Non-idepoment methos should allow client to cache responses.
    • Clients should honor HTTP headers with respect to caching.
  • Layered system
    • Client should not care whether it is connected.
  • Uniform Interface
    • Identification of resources
    • Manipulation of resources through representations
    • Self-descriptive messages
    • Hypermedia as the engine of application state.

ZF2 Fundamentals:
   • PHP 5.3.3+ (and tested on 5.4, as well as upcoming
5.5)
• Modular & flexible (ModuleManager)
• Event-driven (EventManager)
• Leverage Inversion of Control (ServiceManager)
• Re-written MVC, Forms, I18n, Db, and more


Event Example   by  Rob Allen:
Simple example
 use Zend\EventManager\EventManager,
 Zend\EventManager\Event;
 $callback = function($event) {
 echo "An event has happened!\n";
 var_dump($event->getName());
 };
 $events = new EventManager();
 $events->attach('eventName', $callback);
 echo "\nRaise an event\n";
 $events->trigger('eventName');

LISTENER
Listeners
Just a function (Any callback)
 $callback = function($event) {
 echo "An event has happened!\n";
 var_dump($event->getName());
 var_dump($event->getTarget());
 var_dump($event->getParams());
 };
 $events = $someObject->getEventManager();
 $events->attach('eventName', $callback);

TARGET
Compose an EventManager within a class…
use Zend\EventManager\EventManager,
 Zend\EventManager\Event;
class MyTarget
{
 public $events;
 public function __construct()
 {
 $this->events = new EventManager();
 }
… and trigger actions within methods.
 public function doIt()
 {
 $event = new Event();
 $event->setTarget($this);
 $event->setParam('one', 1);
 $this->events->trigger('doIt.pre', $event);
 // do something here
 $this->events->trigger('doIt.post', $event);
 }Typical usage
$callback = function ($event) {
 echo "Responding to doIt.pre!\n";
 var_dump(get_class($event->getTarget()));
 var_dump($event->getName());
 var_dump($event->getParams());
};

$target= new MyTarget();
$target->events->attach('doIt.pre', $callback);
$target->doIt();





More:




Bibliography 


Abeysinghe, Samisa. RESTful PHP Web Services.
Olton Birmingham, GBR: Packt Publishing Ltd, 2008. p 13.
http://site.ebrary.com/lib/univeraguascalientes/Doc?id=10448335&ppg=26
Copyright © 2008. Packt Publishing Ltd. All rights reserved. 

1 comment:

  1. http://docs.oracle.com/javaee/7/tutorial/doc/jaxrs.htm

    ReplyDelete