Middleware

Middleware in Assegai is a way to intercept HTTP requests and responses in order to perform specific actions. This can be useful for a variety of purposes, such as logging requests, authenticating users, or modifying request and response data.

Creating middleware

To create a middleware in Assegai, you will need to create a class that implements the IMiddleware interface. This interface requires you to implement a use method, which takes in a Request object, a Response object, and a callable representing the next middleware in the pipeline. Your middleware should perform its desired actions, then call the $next callable to pass control to the next middleware.

Here is an example of a basic middleware that logs the request and response:

src/Util/Logging/LoggerMiddleware.php
use Assegai\Core\Attributes\Injectable;
use Assegai\Core\Http\Requests\Request;
use Assegai\Core\Http\Responses\Response;
use Assegai\Core\Interfaces\IMiddleware;

#[Injectable]
class LoggerMiddleware implements IMiddleware
{
  function use(Request $request, Response $response, callable $next): void
  {
    // Log the request & response    error_log('Request: ' . $request);
    error_log('Response: ' . $response);

    // Call the next middleware    $next();
  }
}

Registering Middleware

To register middleware in Assegai, you will need to create a new instance of your middleware class and specify the routes that it should be applied to. This can be done in the config property of the #[Module()] attribute. The middleware key of the config array should contain an array of middleware objects. Each object defines the following:

Class Definition
class Specifies the class name of the middleware.
routes Specifies an array of route strings that the middleware should be applied to.
methods [Optional] Specifies which HTTP methods the middleware should be applied to. If not specified, the middleware will be applied to all HTTP methods.

Here is an example of how you might register the LoggerMiddleware class to be applied to all posts routes:

AppModule.php
#[Module(
  ...,
  config: [
    ...,
    'middleware' => [
      ['class' => LoggerMiddleware::class, 'routes' => ['posts']]
    ]
  ]
)]
class AppModule
{
}
Tip you can also specify a route key instead of a routes key to apply the middleware to a single route.

Here is an example of how you might register the LoggerMiddleware class to be applied to the /posts/:id route for GET and POST requests:

src/AppModule.php
#[Module(
  ...,
  config: [
    ...,
    'middleware' => [
      ['class' => LoggerMiddleware::class, 'route' => '/posts/{id}', 'methods' => ['GET', 'POST']]
    ]
  ]
)]
class AppModule
{
}

You can register as many middleware classes as you like in this way. Simply add additional objects to the middleware array.

Once you have registered your middleware, it will be applied to the specified routes whenever a request is made to those routes. You can then use the middleware to perform any additional logic or manipulation of the request and response objects.