Welcome Guest, Not a member yet? Register   Sign In
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

(This post was last modified: 05-02-2020, 01:56 AM by vincent78.)

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB