Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Creating an API with Expressive

4,552 views

Published on

Sample of API using Zend Expressive

Published in: Technology

Creating an API with Expressive

  1. 1. Creating an API with Expressive
  2. 2. Who?
  3. 3. Elton Minetto Developer since 1997 Teacher since 2004 Co-founder at Coderockr since 2010 Zend Framework Evangelist Author of http://bit.ly/zf2napratica and http://bit.ly/ doctrinenapratica @eminetto
  4. 4. API
  5. 5. What?
  6. 6. "[] a set of routines, protocols, and tools for building software applications. An API expresses a software component in terms of its operations, inputs, outputs, and underlying types []" Wikipedia
  7. 7. Why?
  8. 8. Multiple interfaces (web, mobile, CLI)
  9. 9. Integration with internal services Sofware/Infrastructure as a Service (Amazon, Parse, Pusher, Filepicker, etc)
  10. 10. Example
  11. 11. RestBeer
  12. 12. API about 🍻 Multiple response formats (JSON, HTML) http://restbeer.com/beer http://restbeer.com/style http://restbeer.com/beer/Guinness http://restbeer.com/beer/Heineken http://restbeer.com/style/Pilsen http://restbeer.com/style/Stout
  13. 13. Expressive
  14. 14. "Expressive allows you to write PSR-7 middleware applications for the web. It is a simple micro-framework built on top of Stratigility, providing: Dynamic routing Dependency injection via container-interop Templating Error Handling" http://devzone.zend.com/6615/announcing-expressive/
  15. 15. Installing
  16. 16. composer.json { "require": { "zendframework/zend-expressive": "^0.1.0", "aura/router": "^2.3", "zendframework/zend-servicemanager": "^2.6" } } Installing curl -s http://getcomposer.org/installer | php php composer.phar install
  17. 17. Show 🍻
  18. 18. <?php use AuraRouterRouterFactory; use ZendExpressiveAppFactory; use ZendExpressiveRouterAura as AuraBridge; require 'vendor/autoload.php'; $auraRouter = (new RouterFactory())->newInstance(); $router = new AuraBridge($auraRouter); $app = AppFactory::create(null, $router); $beers = array( 'brands' => array('Heineken', 'Guinness', 'Skol', 'Colorado'), 'styles' => array('Pilsen' , 'Stout') ); $app->get('/', function ($request, $response, $next) { $response->getBody()->write('Hello, beers of world!'); return $response; }); $app->get('/brand', function ($request, $response, $next) use ($beers) { $response->getBody()->write(implode(',', $beers['brands'])); return $response; }); $app->get('/style', function ($request, $response, $next) use ($beers) { $response->getBody()->write(implode(',', $beers['styles'])); return $response; }); $app->run();
  19. 19. Testing
  20. 20. php -S localhost:8080
  21. 21. Show a 🍺
  22. 22. $app->get('/beer{/id}', function ($request, $response, $next) use ($beers) { $id = $request->getAttribute('id'); if ($id == null) { $response->getBody()->write( implode(',', $beers['brands']) ); return $response; } $key = array_search($id, $beers['brands']); if ($key === false) { $response->getBody()->write('Not found'); return $response; } $response->getBody()->write($beers['brands'][$key]); return $response; });
  23. 23. JSON!
  24. 24. https://gist.github.com/eminetto/5477c1da39f36d061fee
  25. 25. Authentication
  26. 26. index.php https://gist.github.com/eminetto/197e48af07ad75db0fc2 srcRestBeerAuth.php https://gist.github.com/eminetto/26a2e40f63fb4f1c7546
  27. 27. Add a 🍺
  28. 28. https://gist.github.com/eminetto/f9e44c18314b7aaaf66b
  29. 29. HTML and JSON!
  30. 30. { "require": { "zendframework/zend-expressive": "^0.1.0", "aura/router": "^2.3", "zendframework/zend-servicemanager": "^2.6", "twig/twig": "*" } }
  31. 31. <!-- views/content.twig --> {% for c in content %} {{c}}<br> {% endfor %}
  32. 32. index.php https://gist.github.com/eminetto/4509a6cc5defe2b49d5a srcRestBeerFormat.php https://gist.github.com/eminetto/88114420d9c2b57708f6
  33. 33. Links
  34. 34. http://zend-expressive.readthedocs.org/en/stable/ https://github.com/zendframework/zend- stratigility
  35. 35. Contact
  36. 36. http://eltonminetto.net @eminetto eminetto@coderockr.com http://asemanaphp.com.br http://coderockr.com

×