セッションが設定されていない場合、ミドルウェアを使用してログインページにリダイレクトしています。認証ミドルウェアSlim 3.0
$app->get('/dashboard', function (Request $request, Response $response, $args) {
include_once('employee-portal/dashboard.php');
return $response;})->add(new AuthMiddleware('counter', true, $app->getContainer()));
と私のミドルウェア:
class AuthMiddleware implements MiddlewareInterface{
private $min_role = 'counter';
private $redirect = true;
private $container = null;
public function __construct($role_required, $login_redirect, $container)
{
$this->min_role = $role_required;
$this->redirect = $login_redirect;
$this->container = $container;
}
public function __invoke($request, $response, $next)
{
if ($this->userIsAuthorised()) {
return $next($request, $response);
} else {
if ($this->redirect) {
/**
* @var \Slim\Router router`
*/
return $response->withRedirect(Details::getBaseUrl() . '/login' . '?ref=' . $request->getUri());
}
return $response->withStatus(401)->write("Sorry boss you are not authorised to see my secret");
}
}
private function userIsAuthorised()
{
if ($this->min_role == 'counter') {
return true;
} else if (SessionManager::isLoggedIn()) {
if ($_SESSION['user_type']=='counter') {
return true;
}
}
return false;
} }
が、これは作品を行いません。 ログインせずにダッシュボードページを見ることもできます。ログイン後も私は$_SESSION['user_type']
セッション変数にアクセスすることはできません。
任意のヘルプは、appriciatedされます。前もって感謝します。
どこでも 'session_start()'は表示されません。 – rndus2r
が名前空間で呼び出されました。 SessionManager :: isLoggedIn(); –
これはミドルウェアクラスで呼び出されていますか? – rndus2r