我在Laravel中配置维护模式。我正在尝试添加IP白名单。
当我运行此代码时:
<?php
namespace App\Http\Middleware;
use Closure;
class CheckForMaintenanceMode
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->app->isDownForMaintenance() &&
!in_array($request->getClientIP(), ['127.0.0.1']))
{
return response('Be right back!', 503);
}
return $next($request);
}
}
我收到此错误:
未定义的属性:App \ Http \ Middleware \ CheckForMaintenanceMode :: $ app
有人可以告诉我这是什么问题吗?
答案 0 :(得分:4)
从Laravel 5.6.21开始,此功能现已内置于Laravel中。 php artisan down
命令现在使用--allow
参数,您可以使用这些参数指定允许访问站点的IP地址。
因此,您只需运行php artisan down --allow=127.0.0.1
。
您使用的是$this->app
,但您的课程没有$app
属性。您可以使用app()
辅助方法,将Application
注入中间件,也可以扩展Laravel的CheckForMaintenanceMode
类,这将为您完成所有这些操作。< / p>
class CheckForMaintenanceMode extends \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Foundation\Application;
class CheckForMaintenanceMode
{
/**
* The application implementation.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->app->isDownForMaintenance() &&
!in_array($request->getClientIP(), ['127.0.0.1']))
{
return response('Be right back!', 503);
}
return $next($request);
}
}
public function handle($request, Closure $next)
{
if (app()->isDownForMaintenance() &&
!in_array($request->getClientIP(), ['127.0.0.1']))
{
return response('Be right back!', 503);
}
return $next($request);
}