Answers for "laravel middleware in constructor"

PHP
1

laravel middleware in constructor

public function __construct(User $user)
{
  	$this->user = $user;
  
    $this->middleware(function ($request, $next) {
        $user = auth()->user();
        if ($user) {
          	$this->user = $user;
        }
      
        return $next($request);
    });
}
Posted by: Guest on September-17-2021
0

laravel middleware

<?php
# Run:
# php artisan make:middleware EnsureTokenIsValid
  
namespace App\Http\Middleware;

use Closure;

class EnsureTokenIsValid
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->input('token') !== 'my-secret-token') {
            return redirect('home');
        }

        return $next($request);
    }
}
Posted by: Guest on August-20-2021
1

laravel middleware

<?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);
    }
}
Posted by: Guest on October-27-2020
0

create middleware laravel

php artisan make:middleware <middleware-name>
Posted by: Guest on February-23-2021
0

laravel middleware

php artisan make:middleware EnsureTokenIsValid
Posted by: Guest on August-31-2021
0

middleware in laravel

<?php

namespace App\Http\Middleware;

use Closure;

class CheckType
{
    public function handle($request, Closure $next)
    {
        // Perform action

        return $next($request);
    }
}
Posted by: Guest on December-11-2020

Browse Popular Code Answers by Language