Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page
  • Installation
  • Usage

Was this helpful?

Edit on GitHub
  1. Components

Http Handler

Simple PSR-15 Http Handler

Installation

composer require sonsofphp/http-handler

Usage

Usage is pretty simple.

<?php

use SonsOfPHP\Component\HttpHandler\HttpHandler;
use SonsOfPHP\Component\HttpHandler\MiddlewareStack;

$stack = new MiddlewareStack();
$stack->add(new RouterMiddleware());
$stack->add(new CookieMiddleware());
$stack->add(function ($request, $handler) {
    // ...
});
// ...

$app = new HttpHandler($stack);
$response = $app->handle($request);

The MiddlewareStack accepts objects that implement Psr\Http\Server\MiddlewareInterface and anonymous functions.

Middleware Priorities

An optional second argument may be passed to the MiddlewareStack which is for the priority of the middleware. Priorities are ordered in ascending order.

<?php

use SonsOfPHP\Component\HttpHandler\MiddlewareStack;

$stack = new MiddlewareStack();
$stack->add(new NotFoundMiddleware(), 1025);
$stack->add(new RouterMiddleware(), 255);
$stack->add(new CookieMiddleware(), -255);
$stack->add(new DefaultMiddleware());

In the above example, the CookieMiddleware will be processed first and NotFoundMiddleware will be processed last.

PreviousHttp FactoryNextHttp Message

Last updated 8 months ago

Was this helpful?

📦