make middleware laravel code example
Example 1: laravel make middleware
php artisan make:middleware CheckAge
Example 2: laravel controller middleware
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('log')->only('index');
$this->middleware('subscribed')->except('store');
}
}
Example 3: middleware command in laravel
php artisan make:middleware NameOfTheMiddleware
Example 4: how to attach the middleware to a route definition
Route::get('profile', function () {
})->middleware('verified');
Example 5: laravel middleware
<?php
namespace App\Http\Middleware;
use Closure;
class CheckAge
{
public function handle($request, Closure $next)
{
if ($request->age <= 200) {
return redirect('home');
}
return $next($request);
}
}
Example 6: middleware in laravel
<?php
namespace App\Http\Middleware;
use Closure;
class BeforeMiddleware
{
public function handle($request, Closure $next)
{
return $next($request);
}
}