Laravel Middleware

Author: Al-mamun Sarkar Date: 2020-07-11 14:39:55

এই লেসনে আমরা দেখবো লারাভেল এ কিভাবে Middleware ব্যবহার করতে হয় 

 

Artisan Command:

php artisan make:middleware TestMiddleware

 

Code:

TestMiddleware:

namespace App\Http\Middleware;

use Closure;
use App\User;

class TestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = User::find(1);
        if ( 'sarkar@gmail.com' == $user->email ) {
            return $next($request);
        } else {
            return redirect()->to('/');
        }   
    }
}

 

web.php

Route::get('one', function() {
    return 'This is first page';
})->middleware('test');

Route::get('two', function() {
    return 'This is second page';
})->middleware('test');;

Route::get('three', function() {
    return 'This is third page';
})->middleware('test');;


Route::middleware('test')->group(function() {
	Route::get('one', function() {
	    return 'This is first page';
	});
	Route::get('two', function() {
	    return 'This is second page';
	});
	Route::get('three', function() {
	    return 'This is third page';
	});
});