首页 > 解决方案 > 类 RedirectIfPasswordNotUpdated 不存在

问题描述

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

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)。我究竟做错了什么?

标签: phplaravellaravel-5.8laravel-middleware

解决方案


如我所见,您尚未middleware classapp\Http\Kernel.php. 注册中间件非常简单,如下所示:

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

推荐阅读