• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
returning redirect from parent's function.

#1
Hi, I want to make simple authentication checking method in BaseController.php so it can be accessed by another controller like this:
PHP Code:
protected function mustLoggedIn()
{
    if (
is_null($this->session->get('logged_in'))) {
        
$this->session->setFlashdata(['error' => 'Maaf, anda harus login dulu']);
        return 
redirect()->to('/login');
        exit();
    }

then accessed like this:
PHP Code:
public function beranda()
{
    
$this->mustLoggedIn();
    return 
view('siswa/beranda');

but it didn't work in CI4. Is my method wrong?
How to implement login check like that in CI4?
Reply

#2
Hello,

The 'exit()' call after the 'return' will never be executed.

PHP Code:
protected function isLoggedIn()
{
    if (is_null($this->session->get('logged_in'))) {
        $this->session->setFlashdata(['error' => 'Maaf, anda harus login dulu']);
        return false;
    } else {
        return true;
    }


PHP Code:
public function beranda()
{
    if ($this->isLoggedIn()) {
        return view('siswa/beranda');
    } else {
        return redirect()->to('/login');
    }


Vincent
Reply

#3
I think you might want to check out Controller Filters. They are designed to handle exactly that type of situation.
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.