微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/php/130475.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐