Upload
chris-tankersley
View
379
Download
0
Embed Size (px)
Citation preview
NomadPHP, March 2016 1
Zend Expressive in 15 MinutesChris Tankersley@dragonmantankNomadPHP, March 2016
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
NomadPHP, March 2016 3
What is it?
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
NomadPHP, March 2016 5
The Microframework
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
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();
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
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'));}
NomadPHP, March 2016 10
PSR-7 Implementation
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
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();
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'));}
NomadPHP, March 2016 14
Middleware
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
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; }}
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; }}
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; }}
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'));}
NomadPHP, March 2016 20
Getting Started
NomadPHP, March 2016 21
Resources• Documentation - https://zend-expressive.readthedocs.org/en/latest/• Skeleton App - https://github.com/zendframework/zend-expressive-
skeleton
• My Demo App - https://github.com/dragonmantank/phparch-zend-expressive• Session Middleware - https://github.com/dragonmantank/
zendexpressive-session-middleware