Transcript
Page 1: Creating an API with Expressive

Creating an API with Expressive

Page 2: Creating an API with Expressive

Who?

Page 3: Creating an API with Expressive

Elton MinettoDeveloper 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

Page 4: Creating an API with Expressive

API

Page 5: Creating an API with Expressive

What?

Page 6: Creating an API with Expressive

"[] 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

Page 7: Creating an API with Expressive

Why?

Page 8: Creating an API with Expressive

Multiple interfaces (web, mobile, CLI)

Page 9: Creating an API with Expressive

Integration with internal services Sofware/Infrastructure as a Service

(Amazon, Parse, Pusher, Filepicker, etc)

Page 10: Creating an API with Expressive

Example

Page 11: Creating an API with Expressive

RestBeer

Page 12: Creating an API with Expressive

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

Page 13: Creating an API with Expressive

Expressive

Page 14: Creating an API with Expressive

"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/

Page 15: Creating an API with Expressive

Installing

Page 16: Creating an API with Expressive

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

Page 17: Creating an API with Expressive

Show 馃嵒

Page 18: Creating an API with Expressive

<?phpuse Aura\Router\RouterFactory;use Zend\Expressive\AppFactory;use Zend\Expressive\Router\Aura 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();

Page 19: Creating an API with Expressive

Testing

Page 20: Creating an API with Expressive

php -S localhost:8080

Page 21: Creating an API with Expressive

Show a 馃嵑

Page 22: Creating an API with Expressive

$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;});

Page 23: Creating an API with Expressive

JSON!

Page 24: Creating an API with Expressive

https://gist.github.com/eminetto/5477c1da39f36d061fee

Page 25: Creating an API with Expressive

Authentication

Page 26: Creating an API with Expressive

index.phphttps://gist.github.com/eminetto/197e48af07ad75db0fc2

src\RestBeer\Auth.phphttps://gist.github.com/eminetto/26a2e40f63fb4f1c7546

Page 27: Creating an API with Expressive

Add a 馃嵑

Page 28: Creating an API with Expressive

https://gist.github.com/eminetto/f9e44c18314b7aaaf66b

Page 29: Creating an API with Expressive

HTML and JSON!

Page 30: Creating an API with Expressive

{ "require": { "zendframework/zend-expressive": "^0.1.0", "aura/router": "^2.3", "zendframework/zend-servicemanager": "^2.6", "twig/twig": "*" }}

Page 31: Creating an API with Expressive

<!-- views/content.twig -->{% for c in content %}{{c}}<br>

{% endfor %}

Page 32: Creating an API with Expressive

index.php https://gist.github.com/eminetto/4509a6cc5defe2b49d5a

src\RestBeer\Format.php https://gist.github.com/eminetto/88114420d9c2b57708f6

Page 33: Creating an API with Expressive

Links

Page 34: Creating an API with Expressive

http://zend-expressive.readthedocs.org/en/stable/ https://github.com/zendframework/zend-stratigility

Page 35: Creating an API with Expressive

Contact

Page 36: Creating an API with Expressive

http://eltonminetto.net @eminetto

[email protected] http://asemanaphp.com.br

http://coderockr.com


Top Related