我想检查用户是否登录。因此我有一个类女巫返回真或假。现在我想要一个检查用户是否登录的中间件。
$app->get('/login', '\Controller\AccountController:loginGet')->add(Auth::class)->setName('login');
$app->post('/login', '\Controller\AccountController:loginPost')->add(Auth::class);
认证类
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, \Slim\Http\Response $response, $next) {
if($this->account->login_check()) {
$response = $next($request, $response);
return $response;
} else {
//Redirect to Homepage
}
}
}
因此,当用户登录时,页面将正确呈现。但是当用户没有被自动化时,我想重定向到主页。但是怎么办?!
$response->withRedirect($router->pathFor('home');
这不行!