Zend\Expressive - höher, schneller, weiter

501 views

Published on

Zend\Expressive ist das neue Zugpferd am Zend Framework 3 Himmel. Das »PSR-7 Middleware Microframework« verspricht den Aufbau von leichtgewichtigen Webanwendungen innerhalb von wenigen Minuten. Wir werfen in dieser Session einen genaueren Blick auf die Möglichkeiten von Zend\Expressive und betrachten auch den Aufbau von komplexeren Middleware Anwendungen. Zudem erläutern wir die Frage, ob die Tage der MVC Frameworks nun gezählt sind.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
501
On SlideShare
0
From Embeds
0
Number of Embeds
51
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Zend\Expressive - höher, schneller, weiter

  1. 1. 1 ZENDEXPRESSIVE höher ­ schneller ­ weiter CODE INSIDE
  2. 2. 2 RALF EGGERT Trainer Berater Autor Insulaner Speaker Entwickler ZF1 seit 2006 ZF2 seit 2012 ZF3 seit 2016 GF Travello GmbH www.ralfeggert.de
  3. 3. 3 1 PSR-7 / Middleware 2 Middleware für Aktionen3 ZendExpressive Überblick Middleware für die Pipeline4 Und was ist mit Tee MVC?5
  4. 4. 4 1 PSR-7 / Middleware
  5. 5. 5 WAS IST PSR-7?
  6. 6. 6 PSR­4 Autoload PSR­2 CodingPSR­1 Coding PSR­3 Logging PSR­6 Caching PSR­7 HTTP PHP-FIG www.php­fig.org PSR­11 ??? PSR­14 ??? PSR­15 ???
  7. 7. 7 <?php namespace PsrHttpMessage; interface MessageInterface { public function getProtocolVersion(); public function withProtocolVersion($version); public function getHeaders(); public function hasHeader($name); public function getHeader($name); public function getHeaderLine($name); public function withHeader($name, $value); public function withAddedHeader($name, $value); public function withoutHeader($name); public function getBody(); public function withBody(StreamInterface $body); } PSR-7 MESSAGEINTERFACE
  8. 8. 8 <?php namespace PsrHttpMessage; interface RequestInterface extends MessageInterface {} interface ServerRequestInterface extends RequestInterface {} interface ResponseInterface extends MessageInterface {} interface StreamInterface {} interface UploadedFileInterface {} interface UriInterface{} PSR-7 WEITEREINTERFACES
  9. 9. 9 WAS IST MIDDLEWARE?
  10. 10. 10 CLIENT WEBSERVER HTTP REQUEST HTTP RESPONSE HTTP
  11. 11. 11 MIDDLEWARE HTTP REQUEST HTTP RESPONSE MIDDLEWARE
  12. 12. 12 MIDDLEWARE PIPELINE HTTP REQUEST HTTP RESPONSE MIDDLEWARE 1 MIDDLEWARE 2 MIDDLEWARE 3
  13. 13. 13 <?php interface LambdaMiddlewareInterface { /** * @param RequestInterface $request * @return ResponseInterface */ public function __invoke($request); } LAMBDA MIDDLEWARE INTERFACE
  14. 14. 14 <?php interface InjectedResponseMiddlewareInterface { /** * @param RequestInterface $request * @param ResponseInterface $response * @return ResponseInterface */ public function __invoke($request, $response); } INJECTEDRESPONSE MIDDLEWARE INTERFACE
  15. 15. 15 <?php interface InjectedNextMiddlewareInterface { /** * @param RequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface */ public function __invoke( $request, $response, $next = null ); } INJECTEDNEXT MIDDLEWARE INTERFACE used by ZF3
  16. 16. 16 2 ZendExpressive Überblick
  17. 17. 17 FRAMEWORK SILOS
  18. 18. 18 FRAMEWORK SILOS
  19. 19. 19 ZEND DIACTOROS ZENDSTRATIGILITY ZENDEXPRESSIVE ZF KOMPONENTEN PSR-7 MIDDLEWARE MIDDLEWARE APPLICATIONS
  20. 20. 20 ROUTER DI CONTAINER TEMPLATE RENDERER ERROR HANDLER Aura.Router FastRoute ZendRouter Weitere Router Aura.DI Pimple­interop Zend ServiceManager Weitere DI Container Plates Twig ZendView Weitere Template­Engine Whoops Weiterer Error­Handler ZENDEXPRESSIVE ZUTATEN
  21. 21. 21 PERFORMANCE MESSDATEN Gemessen im April 2016 mit der ZendExpressive Skeleton 1.0.1 AD = Aura.DI, AR = Aura.Router, FR = FastRoute, ZR = ZendRouter, ZS = ZendServiceManager, ZV = ZendView Laufzeit (ms) 31,8 106,5 43,8 31,9 103,8 44,9 42,7 117,2 56,1 35,6 31,3 Router FR FR FR AR AR AR ZR ZR ZR FR FR DI Container ZS ZS ZS ZS ZS ZS ZS ZS ZS AD Pimple Renderer Plates Twig ZV Plates Twig ZV Plates Twig ZV Plates Plates
  22. 22. 22 PERFORMANCE ERKENNTNISSE AD = Aura.DI, AR = Aura.Router, FR = FastRoute, ZR = ZendRouter, ZS = ZendServiceManager, ZV = ZendView AR FR ZR ROUTER Plates Twig ZV RENDERER AD Pimple ZS DI CONTAINER
  23. 23. 23 PERFORMANCE FAZIT AD = Aura.DI, AR = Aura.Router, FR = FastRoute, ZR = ZendRouter, ZS = ZendServiceManager, ZV = ZendView FR SCHNELLSTE VARIANTE PlatesPimple PS: Traue keiner Statistik, die du nicht selber gefälscht hast! ;­) ZR REINE ZF VARIANTE ZVZS
  24. 24. 24 INSTALLATION $ composer create-project ⏎ zendframework/zend-expressive-skeleton ⏎ /var/www/zend-expressive-skeleton $ cd /var/www/zend-expressive-skeleton $ composer serve
  25. 25. 25 ZENDEXPRESSIVE SKELETON APPLICATION
  26. 26. 26 VERZEICHNISSTRUKTUR  config  autoload  data  cache  public  src  Application  Action  templates  application  error  layout  test  ApplicationTest  Action  vendor  composer.json  config  autoload  data  cache  modules  Application  config  src  Action  templates  application  error  layout  test  Action  public  vendor  composer.json MODULAR EINFACH
  27. 27. 27 CONFIGMANAGER $ composer require mtymek/expressive-config-manager <?php // Datei /config/config.php use ZendExpressiveConfigManagerConfigManager; use ZendExpressiveConfigManagerPhpFileProvider; $configManager = new ConfigManager([ ApplicationConfigProvider::class, new PhpFileProvider( 'config/autoload/{{,*.}global,{,*.}local}.php' ), ]); return new ArrayObject( $configManager->getMergedConfig() );
  28. 28. 28 CONFIGPROVIDER <?php // Datei /modules/Application/src/ConfigProvider.php namespace Application; use ZendConfigFactory; class ConfigProvider { public function __invoke() { return Factory::fromFile( __DIR__ . '/../config/module.config.php' ); } }
  29. 29. 29 COMPONENT INSTALLER $ composer require ⏎ zendframework/zend-component-installer $ composer require zendframework/zend-db <?php // Datei /config/config.php use ZendExpressiveConfigManagerConfigManager; use ZendExpressiveConfigManagerPhpFileProvider; $configManager = new ConfigManager([ ZendDbConfigProvider::class, ApplicationConfigProvider::class, new PhpFileProvider( 'config/autoload/{{,*.}global,{,*.}local}.php' ), ]); return new ArrayObject( $configManager->getMergedConfig() );
  30. 30. 30 Middleware für Aktionen3
  31. 31. 31 AKTIONEN BEISPIELE
  32. 32. 32 TEMPLATE BAUSTEINE AKTION ROUTING MIDDLEWARE KONFIGURATION
  33. 33. 33 MIDDLEWARE namespace PizzaAction; use ApplicationTemplateTemplateRendererInterface; use PizzaModelRepositoryPizzaRepositoryInterface; class ShowIntroAction { /* ... */ public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null ) { $pizzas = $this->pizzaRepository->getPizzas(); return new HtmlResponse( $this->renderer->render( 'pizza::intro', ['pizzas' => $pizzas] ) ); } }
  34. 34. 34 ZENDVIEW TEMPLATE <?php foreach ($this->pizzas as $pizza) : ?> <?php $urlShow = $this->url( 'pizza-show', ['id' => $pizza['id']] ); ?> <div class="col-md-4"> <div class="thumbnail text-center"> <a href="<?= $urlShow; ?>"> <img src="<?= $pizza['image'] ?>" title="<?= $pizza['name'] ?>"> </a> </div> </div> <?php endforeach ?>
  35. 35. 35 return [ 'routes' => [ [ 'name' => 'pizza-intro', 'path' => '/pizza', 'middleware' => PizzaActionShowIntroAction::class, 'allowed_methods' => ['GET'], ], [ 'name' => 'pizza-handle-delete', 'path' => '/pizza/delete/:id', 'middleware' => PizzaActionDeletePizzaAction::class, 'allowed_methods' => ['POST'], 'options' => [ 'constraints' => [ 'id' => '[1-9][0-9]*', ], ], ], ], ]; ZENDROUTER ROUTING
  36. 36. 36 namespace PizzaRestAction; class GetIdAction { use PizzaRepositoryAwareTrait; public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null ) { $pizza = $this->pizzaRepository->getSinglePizza( $request->getAttribute('id') ); if (!$pizza) { return new JsonResponse( ['err' => 'Not found'] ); } return new JsonResponse($pizza); } } RESTAKTION
  37. 37. 37 Middleware für die Pipeline4
  38. 38. 38 HTTP REQUEST HTTP RESPONSE ROUTING MIDDLEWARE URL HELPER MIDDLEWARE DISPATCHING MIDDLEWARE ZENDEXPRESSIVE MW PIPELINE
  39. 39. 39 HTTP REQUEST HTTP RESPONSE LOCALIZATION MIDDLEWARE AUTHENTICATION MIDDLEWARE AUTHORIZATION MIDDLEWARE KOMPLEXERE MW PIPELINE
  40. 40. 40 use ZendExpressiveContainerApplicationFactory; use ZendExpressiveHelper; return [ 'middleware_pipeline' => [ 'always' => [ 'middleware' => [ HelperServerUrlMiddleware::class, ], 'priority' => 10000, ], 'routing' => [ 'middleware' => [ ApplicationFactory::ROUTING_MIDDLEWARE, HelperUrlHelperMiddleware::class, ApplicationFactory::DISPATCH_MIDDLEWARE, ], 'priority' => 1, ], 'error' => [ 'middleware' => [], 'error' => true, 'priority' => -10000, ], ], ]; DEFAULT KONFIGURATION
  41. 41. 41 use I18nMiddlewareLocalizationMiddleware; use UserAuthorizationAuthenticationMiddleware; use UserAuthorizationAuthorizationMiddleware; use ZendExpressiveContainerApplicationFactory; use ZendExpressiveHelper; return [ 'middleware_pipeline' => [ 'always' => [ /* ... */ ], 'routing' => [ 'middleware' => [ ApplicationFactory::ROUTING_MIDDLEWARE, HelperUrlHelperMiddleware::class, LocalizationMiddleware::class, AuthenticationMiddleware::class, AuthorizationMiddleware::class, ApplicationFactory::DISPATCH_MIDDLEWARE, ], 'priority' => 1, ], 'error' => [ /* ... */ ], ], ]; KOMPLEXERE KONFIGURATION
  42. 42. 42 namespace UserAuthorization; class AuthorizationMiddleware { public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next ) { $permission = $result->getMatchedRouteName(); if (!$this->rbac->isGranted($this->role, $permission)) { if ($this->role == GuestRole::NAME) { throw new RuntimeException( 'Nicht angemeldet', 401 ); } else { throw new RuntimeException('Kein Zugriff', 403); } } return $next($request, $response); } } AUTHORIZATION MIDDLEWARE
  43. 43. 43 AUTHORIZATION FEHLGESCHLAGEN
  44. 44. 44 Und was ist mit Tee MVC?5
  45. 45. 45 WARUM NOCH MVC, WENN WIR NUN MIDDLEWARE HABEN?
  46. 46. MVCMW Neues Konzept Wenige Module Middleware Pipeline Schwerere Integration unerfahrene Entwickler Neue Projekte Migration komplexer sehr performant erprobt & stabil viele Module Migration einfach Leichte Integration erfahrene Entwickler Event Manager Bestands­ projekte weniger performant Zukunft Gegenwart
  47. 47. 47 TUTORIAL https://github.com/RalfEggert/zend­expressive­tutorial
  48. 48. 48 ACHTUNG! WERBUNG! www.zendframeworkbuch.de
  49. 49. 49 DANKE! FRAGEN?
  50. 50. 50 1 Digging In von Zach Dischner Flickr CC BY 2.0 5 Pipes von Leonid Mamchenkov Flickr CC BY 2.0 17 Monument valley von Moyan Brenn Flickr CC BY 2.0 30 »and... action« von Latin Snake Flickr CC BY 2.0 44 Und was ist mit Tee? aus der Giotto Werbung YouTube 37 Pipeline von jasonwoodhead23 Flickr CC BY 2.0 4 A flowery meadow von Michael Figiel Flickr CC BY 2.0 BILDNACHWEIS 49 free high res texture 380 von Caleb Kimbrough Flickr CC BY 2.0

×