我想检查用户是否登录。因此,我有一个class开关,返回true或false。现在我想要一个中间件来检查用户是否登录
$app->get('/login', 'ControllerAccountController:loginGet')->add(Auth::class)->setName('login');
$app->post('/login', 'ControllerAccountController:loginPost')->add(Auth::class);
Auth类
class Auth {
protected $ci;
private $account;
//Constructor
public function __construct(ContainerInterface $ci) {
$this->ci = $ci;
$this->account = new Account($this->ci);
}
public function __invoke($request, SlimHttpResponse $response, $next) {
if($this->account->login_check()) {
$response = $next($request, $response);
return $response;
} else {
//Redirect to Homepage
}
}
}
因此,当用户登录时,页面将正确呈现。但是当用户没有被自动重定向时,我想重定向到主页。但是怎么回事?!
$response->withRedirect($router->pathFor('home');
这行不通!
您需要return
响应。不要忘记request
和response
对象是不可变的。
return $response = $response->withRedirect(...);
我有一个类似的auth中间件,我就是这样做的,它还添加了一个403(未授权)头。
$uri = $request->getUri()->withPath($this->router->pathFor('home'));
return $response = $response->withRedirect($uri, 403);
在tflight的答案的基础上,您需要执行以下操作才能使一切按预期工作。我试图将其作为修订版提交,因为tflight的答案中提供的代码无法在框架上开箱即用,但被拒绝了,因此在另一个答案中提供:
您需要在中间件中添加以下内容:
protected $router;
public function __construct($router)
{
$this->router = $router;
}
此外,在声明中间件时,您需要添加以下构造函数:
$app->getContainer()->get('router')
类似于:
$app->add(new YourMiddleware($app->getContainer()->get('router')));
如果没有这些更改,解决方案将无法工作,并且您将得到一个错误,即$this->路由器不存在。
有了这些更改,您就可以使用tflight 提供的代码
$uri = $request->getUri()->withPath($this->router->pathFor('home'));
return $response = $response->withRedirect($uri, 403);
制作基本的Middleware
并将$container
注入其中,以便所有中间件都可以扩展它。
Class Middleware
{
protected $container;
public function __construct($container)
{
$this->container = $container;
}
public function __get($property)
{
if (isset($this->container->{$property})) {
return $this->container->{$property};
}
// error
}
}
确保您的Auth
中间件与基本中间件位于同一文件夹中,或者您可以使用名称空间。
class Auth extends Middleware
{
public function __invoke($request, $response, $next)
{
if (!$this->account->login_check()) {
return $response->withRedirect($this->router->pathFor('home'));
}
return $next($request, $response);
}
}
使用:
http_response_code(303);
header('Location: ' . $url);
exit;