Intro to Middleware
Tags: webdev, beginners, programming, php
Published: 2024-07-06 14:04:14

## What is Middleware?

Middleware is a software design pattern that enables seamless communication and data exchange between different systems, applications, or services. It plays a crucial role in facilitating interactions between disparate components, adding functionality, and enhancing overall system performance.

## The Problem

In our previous project, we encountered an issue where a logged-in user was asked to register again when visiting the registration page. This was due to the lack of middleware implementation, which resulted in a poor user experience.

## Middleware in PHP

In PHP, middleware can be used to handle user registration and login functionality, ensuring a smooth user experience. Middleware acts as a bridge between different components, enabling seamless communication and data exchange.

```php
if ($_SESSION['user'] ?? false){ 
    header('location: /'); 
    exit(); 
}
```
It checks either the user is logged in or not. If not then exit the script to find authenticated user.

## Route Configuration
In the `routes.php` file, we can add a 'guest' key to the route to associate it with the middleware:
```php
$router->get('/register', 'controllers/registration/create.php')->only('guest');
```

## Debugging the Only Method
To check if the project is working as expected, you can add a debug statement in the `only` method:
```php
public function only($key){ 
    dd($key); 
}
```
It shows error as the only method cannot work with null value as it associated with get method and it doesn't return any value. So we have to rewrite the method.

## Rewriting the Add Method

To return all values to the `only` method, we need to rewrite the `add` method in the `router.php` file as:
```php
public function add($method, $uri, $controller) 
{ 
    $this->routes[] = [ 
        'uri' => $uri, 
        'controller' => $controller, 
        'method' => $method, 
        'middleware'=>null 
    ]; 
    return $this; 
}
```
Now we can see the project is working well.

## Only Method
The `only` method in the `router.php` file needs to be modified to accept the middleware key:
```php
public function only($key){ 
    $this->routes[array_key_last($this->routes)]['middleware']=$key; 
    return $this; 
}
```

## Middleware Check

In the `create.php` file, we can check if the user is logged in or a guest using the middleware:
```php
if ($route['middleware']==='guest'){ 
    if($_SESSION['user'] ?? false){ 
        header('location: /'); 
        exit(); 
    } 
} 
if ($route['middleware']==='auth'){ 
    if(! $_SESSION['user'] ?? false){ 
        header('location: /'); 
        exit(); 
    } 
}
```
Only authenticated user can have access to all pages while the guest can access the only limited pages.

## Creating a Middleware Directory
To organize our middleware classes, create a new directory in the core folder named Middleware. As we have to make changes at one point for our relaxation, to save our efforts and time. By this we can make our project easier to understand. In this create 3 different classes.

## Auth Middleware

The `Authenticated.php` file checks if the user is logged in and redirects to the home page if true:
```php
<?php namespace Core\Middleware; 
class Authenticated { 
    public function handle() { 
        if (! $_SESSION['user'] ?? false) { 
            header('location: /'); 
            exit(); 
        } 
    } 
}
```

## Guest Middleware

The `Guest.php` file checks if the user is not logged in and redirects to the home page if true:
```php
<?php namespace Core\Middleware; 
class Guest { 
    public function handle() { 
        if ($_SESSION['user'] ?? false) { 
            header('location: /'); 
            exit(); 
        } 
    } 
}
```

## Middleware Class

The `Middleware.php` file uses a `MAP` constant to map middleware keys to their respective classes. Also checks either the middleware exists or not. If not then show a uncaught exception to user to add middleware in project:
```php
<?php namespace Core\Middleware; 
class Middleware { 
    public const MAP = [ 
        'guest' => Guest::class, 
        'auth' => Authenticated::class 
    ]; 
    public static function resolve($key) { 
        if (!$key) { 
            return; 
        } 
        $middleware = static::MAP[$key] ?? false; 
        if (!$middleware) { 
            throw new \Exception("No matching middleware found for key '{$key}'."); 
        } 
        (new $middleware)->handle(); 
    } 
}
```
Now we can see that by making these changes our project is working well.

I hope that you have clearly understood it.