Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP Middleware: A Journey to the Center of The App

PHP Middleware: A Journey to the Center of The App

Middleware is a term to describe the layers wrapped around your application logic. With the adoption of PSR-15, PHP's common interfaces for handling HTTP requests and middleware components, you can now easily add community-developed Middleware modules to your app or framework or create your own.

What you will learn:

-What is Middleware?
-The journey your HTTP request makes to the center of your application logic
-Why it's useful to encapsulate pieces of your application logic using middleware.

Avatar for Sarah

Sarah

May 22, 2019

Other Decks in Programming

Transcript

  1. Agenda • What middleware is • Why use middleware •

    How to implement middleware • Resources • Cute elephant photos
  2. Wikipedia “Middleware is computer software that provides services to software

    applications beyond those available from the operating system. It can be described as ‘software glue’.”
  3. Red Hat “Middleware is multipurpose software that provides services to

    applications outside of what’s offered by the operating system. Any software between the kernel and user apps can be middleware.”
  4. Microsoft “Middleware is software that lies between an operating system

    and the applications running on it. Essentially functioning as hidden translation layer, middleware enables communication and data management for distributed applications.”
  5. “...it seems that mostly all 'wares' are in the middle

    of something. So, are all things middleware?” - Random Stack Overflow User
  6. PHP Middleworld “In the realm of web application development Middleware

    refers to functions wrapped around business logic. They rely upon the decorator pattern and are composed in a sort of layered stack. You could think of them as onion layers...”
  7. My summary... • Logic wrapped around the core of your

    application • Allows you to manipulate the request and/or response • Like an onion
  8. Common Use Cases for Middleware Session handling Logging Security &

    authentication Dev & debugging Routing Setting response headers
  9. How to write a middleware component? Following the PSR-15 specification

    • Must implement MiddlewareInterface • May create and return a response if conditions aren’t met • Or delegate to the provided handler
  10. How to write a request handler? Also following the PSR-15

    specification • Must implement RequestHandlerInterface • May create and return a response • Or call the next middleware in the stack
  11. How to write a request handler? The Awesome PSR-15 list

    has open source dispatcher examples. Example inspired by lightweight implementation: https://github.com/equip/dispatch/blob/master/src/Handler.php
  12. PSR-7 spec: https://www.php-fig.org/psr/psr-7/ PSR-15 spec: https://www.php-fig.org/psr/psr-15/ PHP Middleworld: https://www.php-middleworld.com/ Awesome

    PSR-15 List of Open Source Middleware: https://github.com/middlewares/awesome-psr15-middlewares Links!
  13. Bonus Elephant Fact Their brains can weigh up to 12

    pounds and are the largest brain of all animals living on land.