![Page 1: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/1.jpg)
RESTful Services madeeasy with ZF2
by Rob Allen and Matthew Weier O’PhinneyJanuary 2013
![Page 2: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/2.jpg)
About usRob Allen
• ZF community team• @akrabat
.Matthew Weier O'Phinney
• ZF project lead• @mwop
![Page 3: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/3.jpg)
Agenda• RESTful fundamentals• Zend Framework 2 fundamentals• RESTful ZF2
![Page 4: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/4.jpg)
Restful fundamentalsREpresentational State Transfer
![Page 5: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/5.jpg)
What is Rest?• An architecture• Centers on the transfer of representations of resources
• A resource is any concept that can beaddressed
• A representation is typically a document thatcaptures the current or intended state of aresource
• A client makes requests of a server when it wants totransition to a new state
![Page 6: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/6.jpg)
Strengths• Loose coupling• Less typing (counter-example: SOAP)• Emphasis on readability; uses nouns and verbs
• HTTP methods as verbs: GET, POST, PUT,DELETE, etc.
• Resources as nouns, and, further, collections
![Page 7: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/7.jpg)
Constraints• Client/Server
• Clients are not concerned with storage,allowing them to be portable.
• Servers are not concerned with UI or user state,allowing scalability.
![Page 8: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/8.jpg)
Constraints• Stateless
• No client context stored between requests.This means no sessions!
![Page 9: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/9.jpg)
Constraints• Cacheable
• Non-idempotent methods should allow clients tocache responses.
• Clients should honor HTTP headers with respectto caching.
![Page 10: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/10.jpg)
Constraints• Layered system
• Client should not care whether it is connecteddirectly to the server, or to an intermediaryproxy.
![Page 11: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/11.jpg)
Constraints• Uniform Interface
• Identification of resources• Manipulation of resources through
representations• Self-descriptive messages• Hypermedia as the engine of application state
(HATEOAS)
![Page 12: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/12.jpg)
Primary aspects of a RESTfulweb service
• Base URI for each resource:http://status.dev:8080/api/status/matthew
• Media type used for representations of the resource• HTTP methods are the set of operations allowed for
the resource• The API must be hypertext driven (i.e., provide links
for allowed state transitions)
![Page 13: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/13.jpg)
Content negotiation• Correctly parse the request
• Read the Content-Type header• Raise “415 Unsupported media type” status if
unsupported
• Correctly create the response
• Read the Accept header• Set the Content-Type header
![Page 14: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/14.jpg)
Hypermedia• What is it?• Why is it important?
![Page 15: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/15.jpg)
What is hypermedia?• Media type used for a representation• The link relations between representations and/or
states
![Page 16: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/16.jpg)
Why is hypermediaimportant?
• Discoverability
![Page 17: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/17.jpg)
JSON and HypermediaJSON does not have a defined way of providinghypermedia links
Options:
• “Link” header (GitHub approach)• application/collection+json• application/hal+json
![Page 18: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/18.jpg)
Link headerLink: <url>; rel="relation"[, ... ]
![Page 19: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/19.jpg)
application/collection+jsonSee http://amundsen.com/media-types/collection/format/
{
"collection":
{
"links": [
{"href": "<uri>", "rel": "relation"}
]
}
}
![Page 20: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/20.jpg)
application/hal+jsonSee http://tools.ietf.org/html/draft-kelly-json-hal-03
{
"_links": {
"relation": {"href": "<uri>"}
}
}
![Page 21: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/21.jpg)
Error reporting• HTTP status: 4xx, 5xx• No further information!• Solution
• application/api-problem+json
![Page 22: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/22.jpg)
application/api-problem+jsonSeehttp://tools.ietf.org/html/draft-nottingham-http-problem-02
{
"describedBy": "<url>",
"title": "generic title of error type",
"httpStatus": <status code>,
"detail": "specific message detailing error"
}
![Page 23: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/23.jpg)
Documenting your API• What operations are available for a given resource?• What do representations look like? How do I need to
form my request? What modifiers might be available?
![Page 24: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/24.jpg)
OPTIONS• Minimally, respond to OPTIONS requests, indicating
HTTP methods allowed via the Allow header.• Potentially include information in the HTTP body.
• http://zacstewart.com/2012/04/14/http-options-method.html• http://vimeo.com/49613738 (“Fun with
OPTIONS” by D. Keith Casey at REST Fest2012)
![Page 25: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/25.jpg)
Using a “describedby” Linkrelation
• Use a Link header with a “describedby” link relationpointing to documentation. Seehttp://www.mnot.net/blog/2012/10/29/NO_OPTIONS
Link: <http://status.dev/api/status/docs.md>; \
rel="describedby"
![Page 26: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/26.jpg)
ZF2 FundamentalsThe next generation of Zend Framework
![Page 27: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/27.jpg)
Highlights• 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
![Page 28: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/28.jpg)
MVC key features• Everything is in a Module• MVC is event driven and uses ServiceManager• Controllers contain actions
• which return data for your view scripts, or aresponse
• View scripts contain display code (e.g. HTML)
![Page 29: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/29.jpg)
Directory structure
![Page 30: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/30.jpg)
Module directory
![Page 31: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/31.jpg)
EventsPublish and listen to events
![Page 32: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/32.jpg)
Events• An object triggers an event• Other objects listen to events
![Page 33: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/33.jpg)
Terminology• An EventManager is an object that holds a collection
of listeners for one or more named events, and whichtriggers events.
• An event is an action.• A listener is a callback that can react to an event.• A Target is an object that creates events
![Page 34: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/34.jpg)
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');
![Page 35: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/35.jpg)
ListenersJust 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);
![Page 36: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/36.jpg)
The targetCompose an EventManager within a class…
use Zend\EventManager\EventManager,
Zend\EventManager\Event;
class MyTarget
{
public $events;
public function __construct()
{
$this->events = new EventManager();
}
//...
![Page 37: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/37.jpg)
The target… 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);
}
![Page 38: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/38.jpg)
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();
![Page 39: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/39.jpg)
Attaching listeners globally• Listeners are used for cross-cutting concerns• You want to set up listeners before you instantiation
of object with event manager• For example: logging, caching
![Page 40: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/40.jpg)
SharedEventManagerAttach a listener to another class’ event manager
$shared = $events->getSharedManager();
// or
$shared = StaticEventManager::getInstance();
$shared->attach('Gallery\\Mapper\\Photo',
'findById.pre', function(Event $e) {
$id = $e->getParam('id');
$message = "Retrieving photo: $id";
MyLogger::log($message);
});
![Page 41: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/41.jpg)
Returned values fromlistenerspublic function doIt()
{
$events = $this->events;
$results = $events->trigger('doIt', $this);
foreach ($results as $result) {
var_dump($result);
}
}
$results are in reverse order(most recently triggered event first)
![Page 42: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/42.jpg)
Short-circuiting$params = array('id' => 1);
$results = $this->events->trigger('doIt.pre',
$this, $params, function ($result) {
if ($result instanceof ResultSet) {
return true;
}
return false;
}
);
if ($results->stopped()) { // We ended early }
![Page 43: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/43.jpg)
Priority• Control the order of execution of listeners• $priority is last parameter to attach()
$events->attach('doIt.pre', $cb, $priority);
• Default is 1
• Larger number increases priority (e.g. 1000)• Smaller number decreases priority (e.g. -500)
![Page 44: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/44.jpg)
ServicesIt (lazily) instantiates and holds objects.
![Page 45: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/45.jpg)
Services• Objects you work with (including Controllers).• Easy to replace alternative implementations.• Clean and simple way to configure dependencies.• Explicit and easy to understand - no magic!• Inversion of Control.
![Page 46: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/46.jpg)
Usage$controller = $sm->get('Gallery\Mapper\Photo');
![Page 47: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/47.jpg)
Types of services• Instances (services)• Constructor-less classes (invokables)• Factories for objects with dependencies (factories)• Aliased services (aliases)• Automated initialization (initializers)• Factories for multiple related objects
(abstract_factories)
![Page 48: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/48.jpg)
Instances// programatically
$sm->setService('foo', $fooInstance);
// configuration
array('services' => array(
'foo' => new Foo(),
));
![Page 49: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/49.jpg)
Invokables// programatically
$sm->setInvokableClass('foo', 'Bar\Foo');
// configuration
array('invokables' => array(
'foo' => 'Bar\Foo',
));
![Page 50: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/50.jpg)
Factories// programatically
$sm->setFactory('foo', function($sm) {
$dependency = $sm->get('Dependency')
return new Foo($dependency);
});
// configuration
array('factories' => array(
'foo' => function($sm) { //.. },
'bar' => 'Some\Static::method',
'baz' => 'Class\Implementing\FactoryInterface',
'bat' => 'Class\Implementing\Invoke',
));
![Page 51: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/51.jpg)
Aliases// programatically
$sm->setAlias('foo_db', 'db_adapter');
// configuration
array('factories' => array(
'foo_db', 'db_adapter', // alias of a service
'bar_db', 'foo_db', // alias of an alias
));
// All the same instance
$db = $sm->get('db_adapter');
$db = $sm->get('foo_db');
$db = $sm->get('bar_db');
![Page 52: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/52.jpg)
Initializers// programatically
$sm->addInitializer($callback);
// configuration
array('initializers' => array(
$instance,
$callback,
'Class\Implementing\InitializerInterface',
'Class\Implementing\Invoke',
));
![Page 53: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/53.jpg)
An initializerfunction($instance, $sm) {
if ($instance instanceof FooAwareInterface) {
return;
}
$instance->setFoo($sm->get('foo'));
},
![Page 54: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/54.jpg)
Abstract factoriesFactory capable of handling multiple services
// programatically
$sm->addAbstractFactory($abstractFactoryInstance);
$sm->addAbstractFactory('FooFactory');
// configuration
array('abstract_factories' => array(
'Class\Implementing\AbstractFactoryInterface',
$someAbstractFactoryInstance,
);
![Page 55: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/55.jpg)
An abstract factoryclass AFactory implements AbstractFactoryInterface
{
public function canCreateServiceWithName(
ServiceLocatorInterface $services,
$name, $requestedName
) {
return in_array($name, array('foo','bar');
}
public function createServiceWithName(/*sig*/)
{
return $name == 'foo' ? new Foo : new Bar;
}
}
![Page 56: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/56.jpg)
Other features• All plugin managers are services managers.• Services are shared - can disable per service.• Manager “peering” is available.
![Page 57: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/57.jpg)
Configuration in practice• A nested array in:
• MyModuleModule::getServiceConfig()• ‘service_manager’ array key in config
• sub-array keys : services, invokables, factories,aliases, initializers, abstract_factories
![Page 58: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/58.jpg)
ModulesRe-usable pieces of functionality for constructing a
more complex application.
![Page 59: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/59.jpg)
ModulesProvide your application with:
• autoloading• configuration• services (inc controllers, plugins, etc.)• event listeners
Reusable between applications - “plug & play”!
![Page 60: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/60.jpg)
What can modules be?Anything!
• Plugins: payment module for e-commerce• View helpers: Markdown support• Themes: CSS files, images, view scripts• Libraries: Doctrine2 integration, RESTful support• Applications: blog, e-commerce platform, CMS
![Page 61: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/61.jpg)
A module is…• A PHP namespace• A class called Module within that namespace
• which provides features to the application
![Page 62: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/62.jpg)
A ZF2 Module <?php
namespace MyModule;
class Module {}
That’s it.
![Page 63: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/63.jpg)
A complete ZF2 modulenamespace EdpMarkdown;
class Module extends
\Zend\View\Helper\AbstractHelper
{
public function getViewHelperConfig() {
return array('services' => array(
'markdown' => $this));
}
public function __invoke($string = null) {
require_once __DIR__ . 'markdown.php';
return Markdown($string);
}
}
![Page 64: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/64.jpg)
ModuleManager• Loads all modules• Triggers an event for each module
• allowing listeners to act on Module classes• Results in calls specific methods within yourModule class
![Page 65: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/65.jpg)
Module methods called• getAutoloaderConfig()
• init()
• onBootstrap()
• Service Manager methods:
• getServiceConfig()
• getControllerConfig()
• getControllerPluginConfig()
• getViewHelperConfig()
![Page 66: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/66.jpg)
Other actions• If LocatorRegisteredInterface is implemented,
then register with the service manager.• All configs are merged together:
1. getConfig() results merged in the ordermodules are loaded.
2. Config glob/static paths are merged.3. The getServiceConfig() (and friends) results
are merged together then merged with theresult of steps 1 and 2.
![Page 67: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/67.jpg)
A typical Module classnamespace My;
class Module {
public function getAutoloaderConfig() {
// return config for autoloader factory
}
public function getConfig() {
return include
__DIR__ . '/config/module.config.php';
}
public function onBootstrap($e) {
// do initialization
}
}
![Page 68: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/68.jpg)
Module best practices• Keep init() and onBootstrap() very lightweight.• Read-only (do not perform writes within modules).• Utilize a vendor prefix (e.g., EdpMarkdown, notMarkdown).
• Do one thing, and do it well.
![Page 69: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/69.jpg)
RESTful ZF2Putting REST & ZF2 together
![Page 70: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/70.jpg)
Foundations• Routing• AbstractRestfulController
• Reacting to request headers• Creating hypermedia payloads• Creating error payloads
![Page 71: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/71.jpg)
Routing• Route to an AbstractRestfulController
implementation
• Allows a single route to manage all HTTPmethods for a given resource
• Use a combination of Literal and/or Segment routes
![Page 72: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/72.jpg)
Sample Route 'status' => array(
'type' => 'Segment',
'options' => array(
'route' => '/api/status[/:id]',
'defaults' => array(
'controller' => 'StatusController',
),
'constraints' => array(
'id' => '[a-f0-9]{40}',
),
),
),
![Page 73: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/73.jpg)
AbstractRestfulController• Maps HTTP methods to individual class methods• Performs basic content-negotiation
(application/www-form-urlencoded and JSONbodies will be parsed and provided as $data)
![Page 74: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/74.jpg)
Mapping methods• GET :: getList() or get($id)• POST :: create($data)• PUT :: replaceList(), update($id, $data)• PATCH :: patch($id, $data)• DELETE :: deleteList(), delete($id)• HEAD :: head($id = null)• OPTIONS :: options()
![Page 75: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/75.jpg)
Selecting an acceptable viewmodel
• Select a view model based on Accept• Attach a view strategy based on view model
![Page 76: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/76.jpg)
AcceptableViewModelSelector• Controller plugin
$criteria = array(
'Zend\View\Model\JsonModel' => array(
'\*/json',
),
);
$model = $this->acceptableViewModel($criteria);
![Page 77: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/77.jpg)
Changing view strategybased on model
• Listen on the controller’s dispatch event
$sharedEvents->attach(
'Zend\Mvc\Controller\AbstractRestfulController',
'dispatch',
$listener
-10
);
![Page 78: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/78.jpg)
Sample listener function (MvcEvent $e) {
$result = $e->getResult();
if (!$result instanceof JsonModel) {
return;
}
$app = $e->getApplication();
$services = $app->getServiceManager();
$strategy = $services->get('ViewJsonStrategy');
$view = $services->get('View');
$view->attach($strategy, 100);
},
![Page 79: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/79.jpg)
Directly examining theAccept header $headers = $request->getHeaders();
if (!$headers->has('Accept')) {
// no Accept header; do default
return;
}
$accept = $headers->get('Accept');
if ($accept->match($mediaType)) {
// we have a match!
return;
}
![Page 80: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/80.jpg)
Hypermedia payloads• Links should be fully qualified: include, scheme,
server, and port if necessary• A self relation is recommended• With paginated sets, include first, last, next, andprev relations
![Page 81: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/81.jpg)
Tools for creating links• The url controller plugin and/or view helper can
generate the path if a route is known.• The serverUrl view helper can generate the
scheme/server/port combination• Paginators can be inspected and used to generate
pagination relations
![Page 82: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/82.jpg)
Generating individual links $path = $urlHelper->fromRoute($route, array(
'id' => $id,
));
$url = $serverUrlHelper->__invoke($path);
![Page 83: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/83.jpg)
Generating paginated links // $page is the current page
// $count is the total number of pages
// $base is the base URL to the resource
$next = ($page == $count) ? false : $page + 1;
$prev = ($page == 1) ? false : $page - 1;
$links = array(
'self' => $base
. (1 == $page ? '' : '?p=' . $page),
);
if ($page != 1) {
$links['first'] = $base;
}
![Page 84: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/84.jpg)
cont…if ($count != 1) {
$links['last'] = $base . '?p=' . $count;
}
if ($prev) {
$links['prev'] = $base
. ((1 == $prev) ? '' : '?p=' . $prev;
}
if ($next) {
$links['next'] = $base . '?p=' . $next;
}
![Page 85: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/85.jpg)
Where to generate links• Controller is easiest, but may not be semantically
correct• View model makes sense, but is hard to inject with
helpers• Renderer makes sense, but likely requires specialized
payloads in the view model• A event listener could process the view model and
inject them; similar issues to the renderer, though.• Choose your poison.
![Page 86: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/86.jpg)
Error payloads• Be consistent• Provide detail• application/api-problem+json is a nice standard
![Page 87: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/87.jpg)
API-Problem payloads• describedby is required. If corresponding to HTTP
status,http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.htmldescribing HTTP status codes is a nice default.• title is also required; again, if corresponding to
HTTP status, use established status descriptions.• httpStatus is not required, but recommended.• detail is your place to provide any additional
information.
![Page 88: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/88.jpg)
Where to generateAPI-Problem payloads
• Typically, within the controller; this is where theerrors happen.
• You may also want listeners on dispatch.error soyou can generate 404 responses in this format.
![Page 89: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/89.jpg)
Practical application• YOU will build a simple “status” API for posting
social status
• “text” representing the status• “user” representing the user posting the status• “timestamp” when the status was created• Collection of statuses by user, in reverse
chronological order
• User is present in the URI
![Page 90: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/90.jpg)
Steps• Create the domain logic (this is the hard part)• Create a route• Create a controller that:
• calls on the domain logic• varies the view model based on the Accept
header• creates API-Problem payloads for errors
• Create a listener for injecting hypermedia links in theview model
![Page 91: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/91.jpg)
Route• /status/:user[/:id]
![Page 92: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/92.jpg)
Controller• Extend AbstractRestfulController
• use AcceptableViewModelSelector to pull arelevant view model based on Accept header;create a special view model type that we canlisten for later.
• set specific variables in the view that we canquery later
• use a special object for indicating errors• set appropriate HTTP status codes
![Page 93: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/93.jpg)
Listener• Listen for our special view model type• If an error is detected:
• Create an API-Problem payload• Set the response status code
• Generate hyperlinks based on whether we have acollection or an individual item.
![Page 94: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/94.jpg)
DemonstrationThis is meant to be alive demo of the finished API, andmaybe some code samples.
![Page 95: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/95.jpg)
PhlyRestfully• Module that does these bits for you• Add it to composer
• “phly/phly-restfully”: “dev-master@dev”
• Provide a resource listener that does the variouspersistence related operations and a route, and go.
![Page 96: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/96.jpg)
ReviewWhat have we learnt today?
![Page 97: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/97.jpg)
Review• REST is an architecture, with lots of recommendations
but no single, canonical methodology• Don’t skimp or skip the documentation!
![Page 98: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/98.jpg)
Review• REST has lots of little details to pay attention to:
• URIs per resource• HTTP methods indicating the operations
available for a resource• Media types indicating resource
representations govern how to parse a requestas well as how to format a response
• Hypermedia links to promote discoverabilityand available state changes
![Page 99: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/99.jpg)
Review• Several emerging standards surrounding specifically
RESTful JSON APIs
• Collection+JSON• Hypertext Application Language (HAL)• API-Problem
![Page 100: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/100.jpg)
Review• ZF2 has a lot of built-in features to help build RESTful
applications
• AbstractRestfulController
• Accept header implementation• Rich HTTP tooling in general• Flexible view layer
![Page 101: RESTful Services made easy with ZF2 - akrabat.com Services made easy with ZF2 by Rob Allen and Matthew Weier O’Phinney January 2013. About us Rob Allen ... web service • Base URI](https://reader034.vdocuments.site/reader034/viewer/2022042203/5ea4c7b183a49103cb6b24b6/html5/thumbnails/101.jpg)
Thank you!https://joind.in/7781
Rob Allen : http://akrabat.com : @akrabatMatthew Weier O’Phinney : http://mwop.net : @mwop