laravel middleware

Solutions on MaxInterview for laravel middleware by the best coders in the world

showing results for - "laravel middleware"
Izabella
24 Jan 2021
1php artisan make:middleware nameOfMiddleware
Martina
27 Nov 2020
1class UserController extends Controller
2{
3    /**
4     * Instantiate a new controller instance.
5     *
6     * @return void
7     */
8    public function __construct()
9    {
10        $this->middleware('auth');
11
12        $this->middleware('log')->only('index');
13
14        $this->middleware('subscribed')->except('store');
15    }
16}
Leni
29 Sep 2020
1public function __construct(User $user)
2{
3  	$this->user = $user;
4  
5    $this->middleware(function ($request, $next) {
6        $user = auth()->user();
7        if ($user) {
8          	$this->user = $user;
9        }
10      
11        return $next($request);
12    });
13}
Lucas
28 Jan 2017
1<?php
2# Run:
3# php artisan make:middleware EnsureTokenIsValid
4  
5namespace App\Http\Middleware;
6
7use Closure;
8
9class EnsureTokenIsValid
10{
11    /**
12     * Handle an incoming request.
13     *
14     * @param  \Illuminate\Http\Request  $request
15     * @param  \Closure  $next
16     * @return mixed
17     */
18    public function handle($request, Closure $next)
19    {
20        if ($request->input('token') !== 'my-secret-token') {
21            return redirect('home');
22        }
23
24        return $next($request);
25    }
26}
Lennart
20 Feb 2018
1<?php
2
3namespace App\Http\Middleware;
4
5use Closure;
6
7class CheckAge
8{
9    /**
10     * Handle an incoming request.
11     *
12     * @param  \Illuminate\Http\Request  $request
13     * @param  \Closure  $next
14     * @return mixed
15     */
16    public function handle($request, Closure $next)
17    {
18        if ($request->age <= 200) {
19            return redirect('home');
20        }
21
22        return $next($request);
23    }
24}
Kari
21 Jul 2019
1php artisan make:middleware EnsureTokenIsValid
similar questions
queries leading to this page
laravel middleware