CodeIgniter Forums
How to redirect from initController? - Printable Version

+- CodeIgniter Forums (
+-- Forum: CodeIgniter 4 (
+--- Forum: CodeIgniter 4 Support (
+--- Thread: How to redirect from initController? (/thread-75780.html)

How to redirect from initController? - falko - 03-16-2020

Hello! I have the following code:

PHP Code:
<?php namespace App\Controllers\Admin;


abstract class 
AdminController extends BaseController
$helpers = ['form'];

    public function 
CodeIgniter\HTTP\RequestInterface $request,
CodeIgniter\HTTP\ResponseInterface $response,
Psr\Log\LoggerInterface $logger
) {
$user_id session()->get('user_id');
        if (empty(

It do not work. How to do this type of authorization in CI 4?

RE: How to redirect from initController? - captain-sensible - 03-17-2020

im no expert but you had no replies. Is the essence of what you want to do is this:

if somebody is not logged in and tries to access a route they get sent back to home?

if so i have dome something similar . I have CI4 in dev but put it up live ; since i couldn't be bothered to set up SSl certificates for localhost. At i have a simple
login page I get their name, password and entered captcha via the form which posts to a route.

All i do if name and password match name and hashed password in database and captcha is correct is set using a session in a controller: $_SESSION['role']="admin";

via a route called newblog
$routes->get('newblog', 'Blog::blogForm');

i display a form that gets the stuff i want to enter.I don't want anybody having access to that route unless they are logged in. I check that using a filter.

public function before(RequestInterface $request)

$logic=isset($_SESSION['role']) ;
return redirect('usrLogin');


//as you can see if S_SESSION['role'] is either null or not set , any user trying to access my newblog gets brushed off back to login .

the instruction to check is via app/Config/Filters.php using

public $filters = [
'myfilter' => ['before' => ['newblog']],


probably i should check that $_SESSION['role'] is set that that it also equals admin, but its early days ,so also have not got around to csrf etc yet either!

RE: How to redirect from initController? - falko - 03-18-2020

Maybe should iniController stop the request if it return a response?

RE: How to redirect from initController? - kilishan - 03-18-2020

InitController was put in place so that it was simpler for people to use the controller's constructor without worrying about the framework. It wasn't intended to be a method that everyone used to do stuff in unless absolutely needed. So - no initController won't catch redirects. That's what filters or even the _remap method are for.