CodeIgniter Forums
How to 'protect' Controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to 'protect' Controller? (/showthread.php?tid=77448)



How to 'protect' Controller? - blaasvaer - 09-02-2020

Hi,

I'm taking serious baby steps here ... none of my 'knowledge' from CI3 seem to apply anywhere in CI4 (I know it's a complete rewrite).



I've created an AdminController which extends the BaseController. In that AdminController I've added a __construct:


Code:
protected $helpers = ['url'];

public function __construct ()
{
    $logged_in = false;

    if ( ! $logged_in ) {
        return redirect('App\Controllers\Home::index');
    }
}


I'm having another controller extends this ... but I simply cannot get my head around WHY it's not redirecting as 'expected'?

What is it that I'm completely missing about CI4 despite having read the docs over and over and over and over again and again and again?


RE: How to 'protect' Controller? - demyr - 09-03-2020

Did you have a chance to check the "redirect" part in the documentation? You should type as , for example,

PHP Code:
// Go to specific UI
return redirect()->to('/admin'); 


Documentation About Redirect


RE: How to 'protect' Controller? - blaasvaer - 09-03-2020

Yes, I've tried ALL sorts of ways I ran into in my search ... I only ended up with this as I struggled my way through the jungle of questions and 'answers' in my online search, as the docs wasn't any help. A pure trial and error scenario ... which, to be honest, isn't a way to develop ANYTHING. It simply surprises me that basic stuff like this is that 'hard' to figure out.

If I slam the above directly into an index method of a Controller it works just fine ... just not when I place it in the __construct of the inherited controller ... I don't get it?


RE: How to 'protect' Controller? - demyr - 09-03-2020

Allright, following your problem (redirect problem in __construct) I have found this :

CI Forum Link


RE: How to 'protect' Controller? - blaasvaer - 09-03-2020

Yes, I just found that as well, thanks.

Tip for old users:
Don't apply ANY of the things you used to in CI3 to CI4 – start over. Warning: even though I'm trying to do that, I do get pretty confused from 'just reading the docs' …