Class RedirectIfPasswordNotUpdated 不存在
Class RedirectIfPasswordNotUpdated does not exist
我使用以下命令创建了自定义中间件
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) 中不起作用。我做错了什么?
据我所知,您尚未在 app\Http\Kernel.php
中注册 middleware class
。注册中间件非常简单,如下所示:
protected $routeMiddleware = [
'middle_name' => \App\Http\Middleware\RedirectIfPasswordNotUpdated::class,
]
我使用以下命令创建了自定义中间件
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) 中不起作用。我做错了什么?
据我所知,您尚未在 app\Http\Kernel.php
中注册 middleware class
。注册中间件非常简单,如下所示:
protected $routeMiddleware = [
'middle_name' => \App\Http\Middleware\RedirectIfPasswordNotUpdated::class,
]