php artisan make:middleware nameOfMiddleware
php artisan make:middleware AdminMiddleware
Route::group(['middleware' => 'AppHttpMiddlewareAdminMiddleware'], function()
{
Route::get ('/admin', ['uses' => 'AdminController@index', 'before' => 'admin']);
});
php artisan make:middleware NameOfTheMiddleware
php artisan make:middleware EnsureTokenIsValid
php artisan make:middleware MiddlewreName
Route::get('/profile', function () {
//
})->middleware('auth');
php artisan make:middleware <middleware-name>
public function handle($request, Closure $next)
{
if ($request->user()->type != 'A')
{
return redirect('home');
}
return $next($request);
}
<?php
namespace AppHttpMiddleware;
use Closure;
class BeforeMiddleware
{
public function handle($request, Closure $next)
{
// Perform action
return $next($request);
}
}
if (Auth::user()->is_admin != 1) {...}
The middleware in web applications is the mid-layer between the HTTP request and the application logic. The middleware process incoming requests and execute the code before the controller's actions.
<?php
namespace AppHttpMiddleware;
use Closure;
class CheckType
{
public function handle($request, Closure $next)
{
// Perform action
return $next($request);
}
}