Middleware


Middleware will allow developers to perform request processing before the response is returned to the user. So with Middleware developers can create a sort of Auth, and so fits with the needs. Writing on progress

What is middleware?

Technically speaking, a middleware is a callable that accepts three arguments:

  1. \Psr\Http\Message\ServerRequestInterface - The PSR7 request object
  2. \Psr\Http\Message\ResponseInterface - The PSR7 response object
  3. callable - The next middleware callable

It can do whatever is appropriate with these objects. The only hard requirement is that a middleware MUST return an instance of \Psr\Http\Message\ResponseInterface. Each middleware SHOULD invoke the next middleware and pass it Request and Response objects as arguments.

How does middleware work?

Different frameworks use middleware differently. Harmony adds middleware as concentric layers surrounding your core application. Each new middleware layer surrounds any existing middleware layers. The concentric structure expands outwardly as additional middleware layers are added.

The last middleware layer added is the first to be executed.

When you run the Harmony application, the Request and Response objects traverse the middleware structure from the outside in. They first enter the outer-most middleware, then the next outer-most middleware, (and so on), until they ultimately arrive at the Harmony application itself. After the Harmony application dispatches the appropriate route, the resultant Response object exits the Harmony application and traverses the middleware structure from the inside out. Ultimately, a final Response object exits the outer-most middleware, is serialized into a raw HTTP response, and is returned to the HTTP client.

Writing on progress

How do I write middleware?

Middleware is a callable that accepts three arguments: a Request object, a Response object, and the next middleware. Each middleware MUST return an instance of \Psr\Http\Message\ResponseInterface.

Create Middleware using Artisan Command

> php artisan make:middleware SomeMiddleware File created (SomeMiddleware.php) in app/Middleware/SomeMiddleware.php

Closure Middleware Example

This example middleware is a Closure.

<?php

namespace App\Middleware;
use App\Middleware\System\Middleware;

class SomeMiddleware extends Middleware
{
    public function __invoke($request, $response, $next)
    {
        /**
         * TODO Tulis middleware anda disini
         */

        $response = $next($request, $response);
        return $response;
    }
}

How do I add middleware?

You may add middleware to a Harmony application, to an individual Harmony application route or to a route group. All scenarios accept the same middleware and implement the same middleware interface.

When you create middleware with Artisan Command then middleware will be registered in app/config/app.middleware.php

<?php

// Definikan middleware buatan anda sendiri dibawah ini
$app->add(new \App\Middleware\SomeMiddleware($container));

if you need further explanation, you can read the documentation in Slim Framework - Middleware


Previous Next
Betta Dev Indonesia .Ltd - © All Right Reserved 2019