Hugo Hamon@hhamon
Silex meetsREST and SOAP
What is Silex?
http://silex.sensiolabs.org
Why choosing Silex?
What’s inside?
The SilexPhilosophy
silex.phar   cache/    logs/     src/   tests/  vendor/     web/
Silex Mantranamespace SymfonyComponentHttpKernel;interface HttpKernelInterface{    (Response) function handle(Request $req...
Request Handlingrequire_once __DIR__./silex.phar;$app = new SilexApplication();$app->get(/hello/{name}, function($name) us...
Request Handlingrequire_once __DIR__./silex.phar;use SymfonyComponentHttpFoundationRequest;use SymfonyComponentHttpFoundat...
$app == DIC
Deploying RESTWeb services
REpresentationalStateTransfer
Architecture       style
DesigningREST APIs
$app->get(/events, function (Request $request) {      $events = array(          array(name => OSIDays, venue => Bangalore)...
$app->post(/event, function (Request $request) use ($app) {      // Get POST data or 400 HTTP response      if (!$data = $...
$app->put(/event/{id}, function ($id) use ($app) {      if (!$data = $request->get(event)) {          return new Response(...
$app->delete(/event/{id}, function ($id) use ($app) {      $event = $app[event_manager]->find($id);      if (!$event) {   ...
Advancedrouting
$app->get(/archive/{year}/{month}, function ($month, $year) {     // ...})->bind(archives)            // Route name->value...
Eventsmanagement
$app->before(function (Request $request) use ($app) {      $user = $request->server->get(PHP_AUTH_USER);      $pwd = $requ...
$app->after(function (Request $request, Response $response) {      // Get URI parameter to determine requested output form...
Exception anderror handling
$app->error(function (Exception $e, $code) {      switch ($code) {          case 400:              $message = Bad request....
$app[debug] = true;
$app->post(/event, function (Request $request) use ($app) {      if (!$event = $request->get(event)) {          $app->abor...
Logging withMonolog
use SilexProviderMonologServiceProvider;$app->register(new MonologServiceProvider(), array(    monolog.logfile    => __DIR...
if ($app[debug]) {    $app[monolog]->addInfo(Testing the Monolog logging.);    $app[monolog]->addDebug(Method foo() was ca...
Databaseinteractionswith Doctrine
use SilexProviderDoctrineServiceProvider;$app->register(new DoctrineServiceProvider(), array(    db.options => array(     ...
$app->get(/events, function () use ($app) {      $query = SELECT id, title, venue FROM events;      $events = $app[db]->fe...
Input validation
use SilexProviderValidatorServiceProvider;$app->register(new ValidatorServiceProvider());
$app[validator]->validate($object);
namespace ConfeetModel;use SymfonyComponentValidatorMappingClassMetadata;use SymfonyComponentValidatorConstraintsNotBlank;...
$app->post(/event, function (Request $request) use ($app) {      if (!$data = $request->get(event)) {          $app->abort...
Templateengine
Twig§ Fast§ Concise and rich syntax§ Automatic output escaping§ Modern features§ Extensible
use SilexProviderTwigServiceProvider;$app->register(new TwigServiceProvider(), array(    twig.path       => __DIR__./../vi...
$app->get(/events.{format}, function ($format) use ($app) {   $events = array(       array(name => OSIDays, venue => Banga...
<!-- views/events.xml.twig --><?xml version="1.0" encoding="utf-8" ?><events>    {% for event in events %}         <event>...
HTTP Caching& ESI
Reverse Proxy Caching
use SilexProviderHttpCacheServiceProvider;$app->register(new HttpCacheServiceProvider(), array(    http_cache.cache_dir =>...
$app->get(/events, function () use ($app) {      $events = array(          array(name => OSIDays, venue => Bangalore),    ...
Edge Side Includes
<!-- views/events.twig --><?xml version="1.0" encoding="utf-8" ?><events>   <esi:include src="/metadata" />   <!-- ... -->...
$app->get(/metadata, function () use ($app) {      return new Response(<meta>...</meta>, 200, array(          Cache-Contro...
Functionaltesting
ClientCrawlerPHPUnit
class EventApiTest extends SilexWebTestCase{    public function testRecentEvents()    {        $client = $this->createClie...
IntegratingZend Soap
$path = __DIR__./../vendor/zend/library;$app[autoloader]->registerNamespace(Zend, $path);
use ZendSoapServer;use ConfeetModelEventService;$app->get(/gateway, function (Request $request) use ($app) {      $server ...
Ques%ons?	   92-98, boulevard Victor Hugo 92 115 Clichy Cedex, France trainings@sensio.com (+33 (0)140 998 211) sensiolabs...
Silex meets SOAP & REST
Silex meets SOAP & REST
Silex meets SOAP & REST
Upcoming SlideShare
Loading in...5
×

Silex meets SOAP & REST

17,854

Published on

Silex is a brand new PHP 5.3 micro framework built on top of the Symfony2 de decoupled components. In this session, we will discover how to build and deploy powerful REST web services with such a micro framework and its embedded tools.

The first part of this talk will introduce the basics of the REST architecture. We fill focus on the main concepts of REST like HTTP methods, URIs and open formats like XML and JSON.

Then, we will discover how to deploy REST services using most of interesting Silex tools like database abstraction layer, template engine and input validation. We will also look at unit and functional testing frameworks with PHPUnit and HTTP caching with Edge Side Includes and Varnish support to improve performances.

Published in: Technology

Transcript of "Silex meets SOAP & REST"

  1. 1. Hugo Hamon@hhamon
  2. 2. Silex meetsREST and SOAP
  3. 3. What is Silex?
  4. 4. http://silex.sensiolabs.org
  5. 5. Why choosing Silex?
  6. 6. What’s inside?
  7. 7. The SilexPhilosophy
  8. 8. silex.phar cache/ logs/ src/ tests/ vendor/ web/
  9. 9. Silex Mantranamespace SymfonyComponentHttpKernel;interface HttpKernelInterface{ (Response) function handle(Request $request);}
  10. 10. Request Handlingrequire_once __DIR__./silex.phar;$app = new SilexApplication();$app->get(/hello/{name}, function($name) use($app) { return Hello . $app->escape($name);});$app->run();
  11. 11. Request Handlingrequire_once __DIR__./silex.phar;use SymfonyComponentHttpFoundationRequest;use SymfonyComponentHttpFoundationResponse;$app = new SilexApplication();$app->get(/hello/{name}, function(Request $request) use($app) { $name = $request->attributes->get(name); return new Response(Hello . $app->escape($name));});
  12. 12. $app == DIC
  13. 13. Deploying RESTWeb services
  14. 14. REpresentationalStateTransfer
  15. 15. Architecture style
  16. 16. DesigningREST APIs
  17. 17. $app->get(/events, function (Request $request) { $events = array( array(name => OSIDays, venue => Bangalore), array(name => PHP Tour, venue => Lille), array(name => Confoo, venue => Montreal), // ... ); return new Response(json_encode($events), 200, array( Content-Type => application/json ));});
  18. 18. $app->post(/event, function (Request $request) use ($app) { // Get POST data or 400 HTTP response if (!$data = $request->get(event)) { return new Response(Missing parameters., 400); } // Persist data to the database $event = new Event() $event->title = $data[title]; $event->venue = $data[venue]; $event->save(); // Trigger redirect to the newly created record URI return $app->redirect(/event/. $event->id, 201);});
  19. 19. $app->put(/event/{id}, function ($id) use ($app) { if (!$data = $request->get(event)) { return new Response(Missing parameters., 400); } if (!$event = $app[event_manager]->find($id)) { return new Response(Event not found., 404); } $event->title = $data[title]; $event->venue = $data[venue]; $event->save(); return new Response(Event updated., 200);});
  20. 20. $app->delete(/event/{id}, function ($id) use ($app) { $event = $app[event_manager]->find($id); if (!$event) { return new Response(Event not found., 404); } $event->delete(); return new Response(Event deleted., 200);});
  21. 21. Advancedrouting
  22. 22. $app->get(/archive/{year}/{month}, function ($month, $year) { // ...})->bind(archives) // Route name->value(year, date(Y)) // Default parameter value->value(month, date(m))->assert(year, d{4}) // Parameter format->assert(month, d{2});
  23. 23. Eventsmanagement
  24. 24. $app->before(function (Request $request) use ($app) { $user = $request->server->get(PHP_AUTH_USER); $pwd = $request->server->get(PHP_AUTH_PW); if ( $app[api_user] !== $user || $app[api_pwd] !== $pwd ) { return new Response(Unauthorized, 403); }});
  25. 25. $app->after(function (Request $request, Response $response) { // Get URI parameter to determine requested output format $format = $request->attributes->get(format); switch ($format) { case xml: $response->headers->set(Content-Type, text/xml); break; case json: $response->headers->set(Content-Type, text/json); break; default: $response->headers->set(Content-Type, text/plain); break; }});
  26. 26. Exception anderror handling
  27. 27. $app->error(function (Exception $e, $code) { switch ($code) { case 400: $message = Bad request.; break; case 404: $message = Page not found.; break; default: $message = Internal Server Error.; } return new Response($message, $code);});
  28. 28. $app[debug] = true;
  29. 29. $app->post(/event, function (Request $request) use ($app) { if (!$event = $request->get(event)) { $app->abort(400, Missing parameters.); } // ... return $app->redirect(/event/. $event->id, 201);});
  30. 30. Logging withMonolog
  31. 31. use SilexProviderMonologServiceProvider;$app->register(new MonologServiceProvider(), array( monolog.logfile => __DIR__./../logs/app.log, monolog.class_path => __DIR__./../vendor/monolog/src,));
  32. 32. if ($app[debug]) { $app[monolog]->addInfo(Testing the Monolog logging.); $app[monolog]->addDebug(Method foo() was called.); $app[monolog]->addWarning(Missing parameter "bar".); $app[monolog]->addError(Class Foo does not exist.);}
  33. 33. Databaseinteractionswith Doctrine
  34. 34. use SilexProviderDoctrineServiceProvider;$app->register(new DoctrineServiceProvider(), array( db.options => array( driver => pdo_mysql, host => localhost, user => root, dbname => event_demo, ), db.dbal.class_path => __DIR__./../vendor/doctrine-dbal/lib, db.common.class_path => __DIR__./../vendor/doctrine-common/lib,));
  35. 35. $app->get(/events, function () use ($app) { $query = SELECT id, title, venue FROM events; $events = $app[db]->fetchAll($query); return new Response(json_encode($events));});
  36. 36. Input validation
  37. 37. use SilexProviderValidatorServiceProvider;$app->register(new ValidatorServiceProvider());
  38. 38. $app[validator]->validate($object);
  39. 39. namespace ConfeetModel;use SymfonyComponentValidatorMappingClassMetadata;use SymfonyComponentValidatorConstraintsNotBlank;use SymfonyComponentValidatorConstraintsMaxLength;class Event extends Model{ private $title; private $venue; static public function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint(title, new NotBlank()); $metadata->addPropertyConstraint(title, new MaxLength(array(limit => 50))); $metadata->addPropertyConstraint(venue, new NotBlank()); }}
  40. 40. $app->post(/event, function (Request $request) use ($app) { if (!$data = $request->get(event)) { $app->abort(400, Missing parameters.); } $event = new Event() $event->setTitle($data[title]); $event->setVenue($data[venue]); if (count($app[validator]->validate($event)) > 0) { $app->abort(400, Invalid parameters.); } $event->save(); return $app->redirect(/event/. $event->id, 201);});
  41. 41. Templateengine
  42. 42. Twig§ Fast§ Concise and rich syntax§ Automatic output escaping§ Modern features§ Extensible
  43. 43. use SilexProviderTwigServiceProvider;$app->register(new TwigServiceProvider(), array( twig.path => __DIR__./../views, twig.class_path => __DIR__./../vendor/twig/lib,));
  44. 44. $app->get(/events.{format}, function ($format) use ($app) { $events = array( array(name => OSIDays, venue => Bangalore), array(name => PHP Tour, venue => Lille), array(name => Confoo, venue => Montreal), // ... ); return $app[twig]->render(events..$format..twig, array( events => $events, ));})->assert(format, xml|json);
  45. 45. <!-- views/events.xml.twig --><?xml version="1.0" encoding="utf-8" ?><events> {% for event in events %} <event> <title>{{ event.title }}</title> <venue>{{ event.venue }}</venue> <begin>{{ event.startAt }}</begin> <end>{{ event.endAt }}</end> </event> {% endfor %}<events>
  46. 46. HTTP Caching& ESI
  47. 47. Reverse Proxy Caching
  48. 48. use SilexProviderHttpCacheServiceProvider;$app->register(new HttpCacheServiceProvider(), array( http_cache.cache_dir => __DIR__./../cache,));
  49. 49. $app->get(/events, function () use ($app) { $events = array( array(name => OSIDays, venue => Bangalore), // ... ); $content = $app[twig]->render(events.twig, array( events => $events, )); return new Response($content, 200, array( Cache-Control => public, s-maxage=3600, Surrogate-Control => content="ESI/1.0", ));});
  50. 50. Edge Side Includes
  51. 51. <!-- views/events.twig --><?xml version="1.0" encoding="utf-8" ?><events> <esi:include src="/metadata" /> <!-- ... --><events>
  52. 52. $app->get(/metadata, function () use ($app) { return new Response(<meta>...</meta>, 200, array( Cache-Control => public, s-maxage=600, ));});
  53. 53. Functionaltesting
  54. 54. ClientCrawlerPHPUnit
  55. 55. class EventApiTest extends SilexWebTestCase{ public function testRecentEvents() { $client = $this->createClient(); $crawler = $client->request(GET, /events.xml); $response = $client->getResponse(); $this->assertTrue($response->isOk()); $this->assertEquals(5, count($crawler->filter(event))); $this->assertRegExp(/OSIDays/, $response->getContent()); ... }}
  56. 56. IntegratingZend Soap
  57. 57. $path = __DIR__./../vendor/zend/library;$app[autoloader]->registerNamespace(Zend, $path);
  58. 58. use ZendSoapServer;use ConfeetModelEventService;$app->get(/gateway, function (Request $request) use ($app) { $server = new Server(); $server->setObject(new EventService($app)); $server->setReturnResponse(true); return new Response($server->handle(), 200, array( Content-Type => text/xml ));});
  59. 59. Ques%ons?   92-98, boulevard Victor Hugo 92 115 Clichy Cedex, France trainings@sensio.com (+33 (0)140 998 211) sensiolabs.com - symfony.com – trainings.sensiolabs.com
  1. A particular slide catching your eye?

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

×