What is laravel middleware?

I am trying to understand how middleware works in Laravel. Here, my class can explain to everyone how his work is.?

<?php

namespace App\Http\Middleware;

use Closure;

class CheckAge
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->age <= 200) {
            return redirect('home');
        }

        return $next($request);
    }

}

thank

+4
source share
4 answers

Middlewareprovide a convenient mechanism for filtering requests HTTPincluded in your application. For example, Laravelincludes Middlewarethat authenticates the user of your application. If the user is not authenticated, Middlewareredirects the user to the login screen. However, if the user is authenticated, it Middlewarewill allow the request to continue working in the application.

Link

HTTP-, .

:

$request->age - , HTTP-, <= 200, .

+5
+1

, ,

public function handle($request, Closure $next)
{
    if ($request->age <= 200) {
        return redirect('home');
    }

    return $next($request);
}

. 200, , . , /about, , /home /about i.e., return $next($request);. Similary auth cors. $request->user->role=='admin' .

return $next($request); ( )

+1

The main task of Middleware is to limit the unwanted action, and here you can check the values ​​entered by the user, and you can only allow their action.

0
source

Source: https://habr.com/ru/post/1673177/


All Articles