I'm working on a project based on codeigniter 4 and
IonAuth
- I used a IonAuth library as Auth System and included it in BaseController
successfully using:
$this->ionAuth = new \IonAuth\Libraries\IonAuth();
- After that i make my Auth controller extends BaseController
without problems.
The question is:
When I check the login in __construct function of any controller I get
Call to a member function loggedIn() on null error.
- when i add $this->ionAuth = new \IonAuth\Libraries\IonAuth(); in __construct function even though I called him in BaseController before,
Error is gone but loggedIn() don't work
.
PHP Code:
<?php namespace App\Controllers;
use App\Models\DashboardModel;
class Dashboard extends BaseController
{
protected $Dashboard;
public function __construct()
{
$this->dashboardModel = new DashboardModel();
$this->ionAuth = new \IonAuth\Libraries\IonAuth();
if (!$this->ionAuth->loggedIn())
{
return redirect()->to(base_url().'/auth/login');
}
}
public function indexOne()
{
return view('pages/indexOne');
}
public function indexTwo()
{
return view('pages/IndexTwo');
}
public function indexThree()
{
return view('pages/indexThree');
}
}
To avoid the error I have to call if (!$this->ionAuth->loggedIn()) at all controll methodes like this:
PHP Code:
<?php namespace App\Controllers;
use App\Models\DashboardModel;
class Dashboard extends BaseController
{
protected $Dashboard;
public function __construct()
{
$this->dashboardModel = new DashboardModel();
}
public function indexOne()
{
if (!$this->ionAuth->loggedIn())
{
return redirect()->to(base_url().'/auth/login');
}
return view('pages/indexOne');
}
public function indexTwo()
{
if (!$this->ionAuth->loggedIn())
{
return redirect()->to(base_url().'/auth/login');
}
return view('pages/IndexTwo');
}
public function indexThree()
{
if (!$this->ionAuth->loggedIn())
{
return redirect()->to(base_url().'/auth/login');
}
return view('pages/indexThree');
}
}
I think that's ugly way, any Solutions please??
Thanks.