php – Slim 3中间件重定向

我想检查用户是否已登录.因此我有一个类巫婆返回true或false.现在我想要一个中间件来检查用户是否已登录.
$app->get('/login','\Controller\AccountController:loginGet')->add(Auth::class)->setName('login');
$app->post('/login','\Controller\AccountController:loginPost')->add(Auth::class);

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');

这不起作用!

您需要返回响应.不要忘记请求和响应对象是不可变的.
return $response = $response->withRedirect(...);

我有一个类似的auth中间件,这就是我这样做,它还添加了403(未授权)标头.

$uri = $request->getUri()->withPath($this->router->pathFor('home'));
return $response = $response->withRedirect($uri,403);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...