RedirectIfPasswordNotUpdated类不存在

时间:2019-07-18 06:57:18

标签: php laravel laravel-5.8 laravel-middleware

我已使用以下命令创建了自定义中间件

php artisan make:middleware RedirectIfPasswordNotUpdated

这是我的中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use App;

class RedirectIfPasswordNotUpdated
{
    public function handle($request, Closure $next)
    {
        if (!App::environment(['production'])) {
            return $next($request);
        }
        $user = Auth::user();
        if (!$user->password_updated_at) {
            return redirect()->route('profile.password.edit')->with([
                'message' => 'Please update your password to proceed',
                'alertType' => 'warning',
            ]);
        }
        if (Carbon::now()->diffInDays(Carbon::parse($user->password_updated_at)) > 90) {
            return redirect()->route('profile.password.edit')->with([
                'message' => 'Your password has expired! Please update your password to proceed',
                'alertType' => 'warning',
            ]);
        }
        return $next($request);
    }
}

我想像下面那样在控制器的构造函数中使用这种中间件

public function __construct()
{
    $this->middleware('auth');
    $this->middleware('RedirectIfPasswordNotUpdated');
}

当我这样做的时候,我得到一个ReflectionException (-1),上面写着

Class RedirectIfPasswordNotUpdated does not exist

这是详细的错误

C:\xampp\htdocs\gmi\vendor\laravel\framework\src\Illuminate\Container\Container.php
    }

    /**
     * Instantiate a concrete instance of the given type.
     *
     * @param  string  $concrete
     * @return mixed
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function build($concrete)
    {
        // If the concrete type is actually a Closure, we will just execute it and
        // hand back the results of the functions, which allows functions to be
        // used as resolvers for more fine-tuned resolution of these objects.
        if ($concrete instanceof Closure) {
            return $concrete($this, $this->getLastParameterOverride());
        }

        $reflector = new ReflectionClass($concrete);

        // If the type is not instantiable, the developer is attempting to resolve
        // an abstract type such as an Interface or Abstract Class and there is
        // no binding registered for the abstractions so we need to bail out.
        if (! $reflector->isInstantiable()) {
            return $this->notInstantiable($concrete);
        }

        $this->buildStack[] = $concrete;

        $constructor = $reflector->getConstructor();

        // If there are no constructors, that means there are no dependencies then
        // we can just resolve the instances of the objects right away, without
        // resolving any other types or dependencies out of these containers.
        if (is_null($constructor)) {
            array_pop($this->buildStack);

            return new $concrete;
        }

我在其他Laravel(v5.4,v5.6)项目中使用此中间件软件的方式相同,没有任何问题。但是在当前版本(v5.8)中不起作用。我在做什么错了?

1 个答案:

答案 0 :(得分:3)

据我所知,您尚未在middleware class中注册app\Http\Kernel.php。注册中间件非常简单,如下所示:

protected $routeMiddleware = [
   'middle_name' => \App\Http\Middleware\RedirectIfPasswordNotUpdated::class,
]