I have these code in the files.
index.php
<?php
$App = new \Slim\App();
$App->get('/', '\Modules\Home\Controllers\IndexController:indexAction');
$App->get('/admin', '\Modules\Admin\Controllers\IndexController:indexAction');
Modules/Home/Controllers/IndexController.php
<?php
namespace Modules\Home\Controllers;
use \Psr\Http\Message\ServerRequestInterface;
use \Psr\Http\Message\ResponseInterface;
/**
* Home main controller.
*
* @author Vee W.
*/
class IndexController extends \Modules\Core\Controllers\BaseController
{
public function indexAction(ServerRequestInterface $request, ResponseInterface $response)
{
// this is home page. just send user to /admin page.
return $response->withStatus(301)->withHeader('Location', 'admin');
}// indexAction
}
Modules/Admin/Controllers/IndexController.php
<?php
namespace Modules\Admin\Controllers;
use \Psr\Http\Message\ServerRequestInterface;
use \Psr\Http\Message\ResponseInterface;
/**
* Admin home page or admin dashboard.
*
* @author Vee W.
*/
class IndexController extends \Modules\Core\Controllers\AdminController
{
public function indexAction(ServerRequestInterface $request, ResponseInterface $response)
{
//echo 'admin page.';
return $response;
}// indexAction
}
Modules/Core/Controllers/AdminController.php
<?php
namespace Modules\Core\Controllers;
use \Psr\Http\Message\ServerRequestInterface;
use \Psr\Http\Message\ResponseInterface;
use \Interop\Container\ContainerInterface;
/**
* The administrator base controller that all admin page should extends this.
*
* @author Vee W.
*/
abstract class AdminController extends \Modules\Core\Controllers\BaseController
{
public function __construct(ContainerInterface $ci)
{
parent::__construct($ci);
$this->ci = $ci;
$UserDb = new \Modules\Admin\Models\UsersDb($this->Db);
if (!$UserDb->isUserLoggedIn()) {
header('Location: admin/login');
http_response_code(302);
}
}// __construct
}
I want to redirect not logged in users from this base controller (Modules/Core/Controllers/AdminController.php) but I cannot redirect the user.
How to redirect from controller constructor?