22
Zend Expressive in 15 Minutes Chris Tankersley @dragonmantank NomadPHP, March 2016 NomadPHP, March 2016 1

Zend Expressive in 15 Minutes

Embed Size (px)

Citation preview

Page 1: Zend Expressive in 15 Minutes

NomadPHP, March 2016 1

Zend Expressive in 15 MinutesChris Tankersley@dragonmantankNomadPHP, March 2016

Page 2: Zend Expressive in 15 Minutes

NomadPHP, March 2016 2

Who Am I• PHP Programmer for over 11 years• Sysadmin/DevOps for around 9 years• https://github.com/dragonmantank

• Author of “Docker for Developers”• Reigning, Defending, Undisputed PHP

MTG Champion of the World

Page 3: Zend Expressive in 15 Minutes

NomadPHP, March 2016 3

What is it?

Page 4: Zend Expressive in 15 Minutes

NomadPHP, March 2016 4

It’s the Future! And Now!• A Microframework• Implements PSR-7• Fully functional Middleware stack• The beginnings of Zend Framework 3

Page 5: Zend Expressive in 15 Minutes

NomadPHP, March 2016 5

The Microframework

Page 6: Zend Expressive in 15 Minutes

NomadPHP, March 2016 6

The Microframework• Lightweight wrapper around other libraries• PSR-7 Compliant• Supports multiple routers• Supports multiple service locators• Supports multiple templating systems

Page 7: Zend Expressive in 15 Minutes

NomadPHP, March 2016 7

<?phpuse Zend\Expressive\AppFactory;

chdir(dirname(__DIR__));require 'vendor/autoload.php';

$app = AppFactory::create();

$app->get('/', function ($request, $response, $next) { $response->getBody()->write('Hello, world!'); return $response;});

$app->pipeRoutingMiddleware();$app->pipeDispatchMiddleware();$app->run();

Page 8: Zend Expressive in 15 Minutes

NomadPHP, March 2016 8

Not just for basic sites• Provides support for Controllers, Dependency Injection, and

Templating• Right now feels somewhere between a microframework and full stack

like ZF2 or symfony2

Page 9: Zend Expressive in 15 Minutes

NomadPHP, March 2016 9

use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ServerRequestInterface;

/** * Renders out the homepage * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable|null $next * * @return HtmlResponse */public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null){ return new HtmlResponse($this->template->render('index::homepage'));}

Page 10: Zend Expressive in 15 Minutes

NomadPHP, March 2016 10

PSR-7 Implementation

Page 11: Zend Expressive in 15 Minutes

NomadPHP, March 2016 11

What is PSR-7?• HTTP Message Interfaces• Provides a consistent interface for HTTP requests and responses• Allows multiple libraries to read and generate HTTP messages more

easily

Page 12: Zend Expressive in 15 Minutes

NomadPHP, March 2016 12

<?phpuse Zend\Expressive\AppFactory;

chdir(dirname(__DIR__));require 'vendor/autoload.php';

$app = AppFactory::create();

$app->get('/', function ($request, $response, $next) { $response->getBody()->write('Hello, world!'); return $response;});

$app->pipeRoutingMiddleware();$app->pipeDispatchMiddleware();$app->run();

Page 13: Zend Expressive in 15 Minutes

NomadPHP, March 2016 13

use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ServerRequestInterface;

/** * Renders out the homepage * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable|null $next * * @return HtmlResponse */public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null){ return new HtmlResponse($this->template->render('index::homepage'));}

Page 14: Zend Expressive in 15 Minutes

NomadPHP, March 2016 14

Middleware

Page 15: Zend Expressive in 15 Minutes

NomadPHP, March 2016 15

Built around Middleware• Middleware interacts with the Request and Response• Can be used for things like authentication, authorization, session

handling… anything “application”-y• Almost everything is middleware, even routes

Page 16: Zend Expressive in 15 Minutes

NomadPHP, March 2016 16

class SessionMiddleware{ protected $sessionContainer;

// ...

public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $request = $request ->withAttribute('session', $this->sessionContainer->getSession());

if ($next) { return $next($request, $response); }

return $response; }}

Page 17: Zend Expressive in 15 Minutes

NomadPHP, March 2016 17

class SessionMiddleware{ protected $sessionContainer;

// ...

public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $request = $request ->withAttribute('session', $this->sessionContainer->getSession());

if ($next) { return $next($request, $response); }

return $response; }}

Page 18: Zend Expressive in 15 Minutes

NomadPHP, March 2016 18

class SessionMiddleware{ protected $sessionContainer;

// ...

public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $request = $request ->withAttribute('session', $this->sessionContainer->getSession());

if ($next) { return $next($request, $response); }

return $response; }}

Page 19: Zend Expressive in 15 Minutes

NomadPHP, March 2016 19

use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ServerRequestInterface;

/** * Renders out the homepage * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable|null $next * * @return HtmlResponse */public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null){ return new HtmlResponse($this->template->render('index::homepage'));}

Page 20: Zend Expressive in 15 Minutes

NomadPHP, March 2016 20

Getting Started

Page 22: Zend Expressive in 15 Minutes

NomadPHP, March 2016 22

http://[email protected]

@dragonmantank

https://joind.in/talk/d9813