Skip to main content

Field note

What is middleware?

PHPLearnRefresh

What is middleware? Asked with 33% probability

Middleware — is software that sits between the web server and the application. It processes incoming requests before they reach your application, or processes outgoing responses after the application generates them.

Why middleware is needed

1. Request management: Can modify or check requests before passing them to the application. For example, it can check for valid access tokens for authentication or perform access rights verification.

2. Response interception: Can also modify responses before they are sent to the user, for example, adding necessary HTTP headers or compressing data.

3. Logging and monitoring: It can record information about requests and responses for traffic analysis or error detection.

4. Error handling: Often used for centralized error handling, allowing errors to be managed elegantly and uniformly.

In a PHP framework such as Laravel, it is often used for authentication, logging, session handling, etc. Below is an example of middleware that checks if a user is logged in in Laravel:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class CheckAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!Auth::check()) {
            return redirect('/login');
        }

        return $next($request);
    }
}

In this example, CheckAuthenticated checks if the user is authenticated. If not, redirects them to the login page.

Middleware — is a layer between the server and application that helps manage requests and responses, provides security, and maintains code cleanliness and modularity. It’s an intermediary that allows you to centrally manage common tasks related to requests and responses.