9

ユーザーがログインしているかどうかを確認したいので、クラスの魔女がtrueまたはfalseを返します。今、ユーザーがログインしているかどうかを確認するミドルウェアが必要です。

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

これはうまくいきません!

4

4 に答える 4

10

対応が必要ですreturnrequestオブジェクトとresponseオブジェクトが不変であることを忘れないでください。

return $response = $response->withRedirect(...);

私は同様の認証ミドルウェアを持っています。これは、403 (未承認) ヘッダーも追加する方法です。

$uri = $request->getUri()->withPath($this->router->pathFor('home'));
return $response = $response->withRedirect($uri, 403);
于 2016-04-09T21:51:42.987 に答える
-1

使用する:

http_response_code(303);
header('Location: ' . $url);
exit;
于 2016-12-06T13:19:47.957 に答える