Middleware PHP
Walter Dal Mut
@walterdalmut - github.com/wdalmut
About Middleware
function($request,$response){}
Something that does something with a Request and a Response
Thanks for listening
Why middleware is interesting?
Service Oriented Architecture
Micro Services
Something that we can change in couple of week...
General principle
Wire components that interacts with the
request and the response
$app = new MiddlewareRunner();
$app­>ad...
A picture is worth a thousand words
from StackPHP
We need a good
implementation for Request
and Response
Existing middleware frameworks
StackPHP
Slim Framework
...
Frankie
A Frankenstein framework
https://github.com/wdalmut/frankie
Different framework components
Frankie is my third framework
Simple-MVC
Push & Pull MVC
UpCloo Web Framework
Event-Driven framework with ZF components
Fr...
Components
acclimate/container
doctrine/annotations
doctrine/cache
doctrine/common
mnapoli/php-di
symfony/routing
symfony/...
Ideas
Middeware approach
Composition (DiC)
Readability (Annotations)
Not event-driven (state-machine approach)
TDD & Code ...
Middleware
class MyController
{
    public function index(Request $request, Response $response, ...$params) { }
}
Composition
Via acclimate (mixing containers)
$sfContainer = new DicBuilder();
$loader = new XmlFileLoader(
    $sfContain...
All is resolved by
Acclimate/PHP-Di
class User
{
    /**
     * @Inject("orm")
     */
    private $entityManager;
    //....
Readability via annotations
/**
 * @Route(name="/v1")
 */
class User
{
    /**
     * @Route(name="/user/{id}", methods={"...
State-Machine
Middle-out - Read the action in order to compose other steps
/**
 * @Route(name="/v1")
 * @Before(targetClas...
Shortcutting
class Auth
{
    public function basic(Request $request, Response $response)
    {
        $auth = $request­>...
Discover more on:
https://github.com/wdalmut/frankie
http://frankie.readthedocs.org/en/develop/
https://github.com/wdalmut...
About PHP and Middleware
https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-
7.html
Check-out an example
Thanks for listening
Nächste SlideShare
Wird geladen in …5
×

Middleware PHP - A simple micro-framework

4.746 Aufrufe

Veröffentlicht am

Mixing other framework components in order to get out a middleware PHP framework

Veröffentlicht in: Software
  • This looks like a good micro framework, but how does it fare against the top PHP micro frameworks (https://www.cloudways.com/blog/install-php-microframeworks-on-cloud-server/ ) like Lumen, Slim and Silex?
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

Middleware PHP - A simple micro-framework

  1. 1. Middleware PHP Walter Dal Mut @walterdalmut - github.com/wdalmut
  2. 2. About Middleware function($request,$response){} Something that does something with a Request and a Response
  3. 3. Thanks for listening
  4. 4. Why middleware is interesting? Service Oriented Architecture Micro Services Something that we can change in couple of weeks etc. etc.
  5. 5. General principle Wire components that interacts with the request and the response $app = new MiddlewareRunner(); $app­>add(new Versioning()); $app­>add(new Router()); $app­>add(new Authentication()); $app­>add(new Options()); $app­>add(new Authorization()); $app­>add(new Accepts()); $app­>add(new ContentType()); $app­>add(new Parser()); $app­>add(new Params()); $app­>add(new Query()); $app­>add(new Body()); $app­>add(new Dispatcher()); $app­>add(new ProblemHandler()); $app­>run($request, $response);
  6. 6. A picture is worth a thousand words from StackPHP
  7. 7. We need a good implementation for Request and Response
  8. 8. Existing middleware frameworks StackPHP Slim Framework ...
  9. 9. Frankie A Frankenstein framework https://github.com/wdalmut/frankie
  10. 10. Different framework components
  11. 11. Frankie is my third framework Simple-MVC Push & Pull MVC UpCloo Web Framework Event-Driven framework with ZF components Frankie Middleware with different framework components https://github.com/wdalmut/simple-mvc https://github.com/wdalmut/upcloo-web-framework https://github.com/wdalmut/frankie
  12. 12. Components acclimate/container doctrine/annotations doctrine/cache doctrine/common mnapoli/php-di symfony/routing symfony/http-foundation symfony/config
  13. 13. Ideas Middeware approach Composition (DiC) Readability (Annotations) Not event-driven (state-machine approach) TDD & Code Design
  14. 14. Middleware class MyController {     public function index(Request $request, Response $response, ...$params) { } }
  15. 15. Composition Via acclimate (mixing containers) $sfContainer = new DicBuilder(); $loader = new XmlFileLoader(     $sfContainer, new FileLocator(realpath(__DIR__  . '/../')) ); $loader­>load(realpath(__DIR__ . '/../configs/services.xml')); $container­>addContainer($acclimate­>acclimate($sfContainer)); //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ $conf = include __DIR__ . '/../configs/services.php'; $serviceManager = new ServiceManager(); $serviceManagerConfigurator = new ServiceManagerConfig($conf["services"]); $serviceManagerConfigurator­>configureServiceManager($serviceManager); $serviceManager­>setService("Config", $conf); $container­>addContainer($acclimate­>acclimate($serviceManager)); Symfony2 DiC and Zend Framework ServiceManager together
  16. 16. All is resolved by Acclimate/PHP-Di class User {     /**      * @Inject("orm")      */     private $entityManager;     //... }
  17. 17. Readability via annotations /**  * @Route(name="/v1")  */ class User {     /**      * @Route(name="/user/{id}", methods={"GET"})      */     public function get(Request $request, Response $response, $id)     {         //...     } } http://domain.tld/v1/user/12
  18. 18. State-Machine Middle-out - Read the action in order to compose other steps /**  * @Route(name="/v1")  * @Before(targetClass="One", targetMethod="one")  * @After(targetClass="Two", targetMethod="two")  * @After(targetClass="Five", targetMethod="five")  */ class User {     /**      * @Route(name="/user/{id}", methods={"GET"})      * @Before(targetClass="Three", targetMethod="three")      * @After(targetClass="Four", targetMethod="four")      */     public function get(Request $request, Response $response, $id) { } } State-Machine: (other objects can chain more steps) One::one => Three::three User::get Four::four => Two::two => Five::five
  19. 19. Shortcutting class Auth {     public function basic(Request $request, Response $response)     {         $auth = $request­>headers­>get("Authorization");         //...         if (!$isValid) {             $response­>setStatusCode(401);             return $response;         }     } } Or more readable return new ApiProblem(401, "Unauthorized"); //ApiProblem extends Response
  20. 20. Discover more on: https://github.com/wdalmut/frankie http://frankie.readthedocs.org/en/develop/ https://github.com/wdalmut/frankie-api https://github.com/wdalmut/frankie-app
  21. 21. About PHP and Middleware https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr- 7.html
  22. 22. Check-out an example
  23. 23. Thanks for listening

×