You've heard of Zend's new framework, Expressive, and you've heard it's the new hotness. In this talk, I will introduce the concepts of Expressive, how to bootstrap a simple application with the framework using best practices, and finally how to integrate a third party tool like Doctrine ORM.
25. @asgrim
Pipe all the things!
$pipe = new ZendStratigilityMiddlewarePipe();
$pipe->pipe($sessionMiddleware);
$pipe->pipe('/foo', $fooMiddleware);
$pipe->pipe($whateverMiddleware);
$pipe->pipe($dogeMiddleware);
31. @asgrim
Expressive installer - start
$ composer create-project zendframework/zend-expressive-skeleton
book-library
Installing zendframework/zend-expressive-skeleton (1.0.3)
- Installing zendframework/zend-expressive-skeleton (1.0.3)
Downloading: 100%
Created project in book-library
> ExpressiveInstallerOptionalPackages::install
Setup data and cache dir
Setting up optional packages
32. @asgrim
Expressive installer - minimal?
Minimal skeleton? (no default middleware, templates, or assets;
configuration only)
[y] Yes (minimal)
[n] No (full; recommended)
Make your selection (No): n
33. @asgrim
Expressive installer - router?
Which router do you want to use?
[1] Aura.Router
[2] FastRoute
[3] Zend Router
Make your selection or type a composer package name and version
(FastRoute): 2
34. @asgrim
Expressive installer - container?
Which container do you want to use for dependency injection?
[1] Aura.Di
[2] Pimple
[3] Zend ServiceManager
Make your selection or type a composer package name and version
(Zend ServiceManager): 3
35. @asgrim
Expressive installer - template?
Which template engine do you want to use?
[1] Plates
[2] Twig
[3] Zend View installs Zend ServiceManager
[n] None of the above
Make your selection or type a composer package name and version
(n): n
36. @asgrim
Expressive installer - whoops?
Which error handler do you want to use during development?
[1] Whoops
[n] None of the above
Make your selection or type a composer package name and version
(Whoops): n
37. @asgrim
Expressive installer - run it!
$ composer serve
> php -S 0.0.0.0:8080 -t public/ public/index.php
[Thu Sep 1 20:29:33 2016] 127.0.0.1:48670 [200]: /favicon.ico
{
"welcome": "Congratulations! You have installed the
zend-expressive skeleton application.",
"docsUrl": "zend-expressive.readthedocs.org"
}
39. @asgrim
Book entity
final class Book
{
/**
* @var string
*/
private $id;
/**
* @var bool
*/
private $inStock = true;
public function __construct()
{
$this->id = (string)Uuid::uuid4();
}
40. @asgrim
Book entity
final class Book
{
/**
* @return void
* @throws AppEntityExceptionBookNotAvailable
*/
public function checkOut()
{
if (!$this->inStock) {
throw ExceptionBookNotAvailable::fromBook($this);
}
$this->inStock = false;
}
41. @asgrim
Book entity
final class Book
{
/**
* @return void
* @throws AppEntityExceptionBookAlreadyStocked
*/
public function checkIn()
{
if ($this->inStock) {
throw ExceptionBookAlreadyStocked::fromBook($this);
}
$this->inStock = true;
}
53. @asgrim
Installation
$ composer require dasprid/container-interop-doctrine
Using version ^0.2.2 for dasprid/container-interop-doctrine
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing dasprid/container-interop-doctrine (0.2.2)
Loading from cache
Writing lock file
Generating autoload files
$
62. @asgrim
Generate the schema
$ vendor/bin/doctrine orm:schema-tool:create
ATTENTION: This operation should not be executed
in a production environment.
Creating database schema...
Database schema created successfully!
$
63. @asgrim
Insert some data
INSERT INTO book (id, name, in_stock) VALUES (
'1c06bec9-adae-47c2-b411-73b1db850e6f',
'The Great Escape',
true
);
77. @asgrim
To summarise...
● PSR-7 is important
● Diactoros is just a PSR-7 implementation
● Stratigility is a middleware pipeline
● Expressive is a glue for container, router (and templating)
● DoctrineModule can be used (with some fiddling)
● container-interop-doctrine makes Doctrine work easily
● Middleware gives you good controls