CodeIgniter Forums
Redirect - 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: Redirect (/showthread.php?tid=72338)



Redirect - oliver - 12-06-2018

Hi,
I have created a BaseController extending CI Controller :

Code:
<?php namespace App\Controllers;

class BaseController extends \CodeIgniter\Controller
{

    protected $env_data;
    protected $env_user;
    
    function __construct()
    {
        $this::init();
    }

    function init() {
        $this->env_data = ['init'=>'yes'];
        $this->env_user = [];

        if(empty($this->env_user)) {
            return redirect()->to('/login');
        }
    }


}
I then created controller Home2 :

Code:
<?php namespace App\Controllers;

class Home2 extends BaseController
{

    public function __construct()
    {
        parent::init();
    }

    public function index()
    {
        print_r($this->env_data);
        
        //return redirect()->to('/login');

        return view('welcome_message', $this->env_data);
    }
}
Calling Home2 displays array $this->env_data containing "init"=>'yes'. The redirection does not work.

Can someone confirm this is the expected result?

Why is redirection not working in BaseController?

Thanks in advance!


RE: Redirect - kilishan - 12-06-2018

The redirect method returns a RedirectResponse class in CI4, which you must return from your methods. In Home2 it's doing parent::init(), but not returning the RedirectResponse if it is returned.

A second issue you'll run into, though, is that redirects cannot be returned from class constructors now, since we are using a class. You should instead look into using Controller Filters for those type of situations.


RE: Redirect - oliver - 12-09-2018

Hi,
Thanks for your answer, Kilishan.
I've read the guide concerning filters : seems to be a real improvement in CI4!
But I still can't get my redirect working.
I created application/Filters/AuthFilter.php :

Code:
<?php namespace App\Filters;

use Config\Services;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;

class AuthFilter implements FilterInterface
{

 public function before(RequestInterface $request)
 {
    echo "filtered";
   return redirect('login');
 }

 public function after(RequestInterface $request, ResponseInterface $response)
 {

 }
}


Then I changed Config/Filters:

Code:
<?php namespace Config;

use CodeIgniter\Config\BaseConfig;

class Filters extends BaseConfig
{
    public $aliases = [
        'csrf'     => \App\Filters\CSRF::class,
        'toolbar'  => \App\Filters\DebugToolbar::class,
        'honeypot' => \App\Filters\Honeypot::class,
        'authfilter' => \App\Filters\AuthFilter::class
    ];

    public $globals = [
        'before' => [
            'authfilter' => ['except'=>['login']]
        ],
        'after'  => [
            'toolbar'
        ],
    ];

    public $methods = [];

    public $filters = [];
}
Which works fine when using login controller directly : the page displays without displaying "filtered".
But when I try to reach Home2::index, only "filtered" is displayed, no redirection takes place :

Code:
<?php namespace App\Controllers;

class Home2 extends BaseController
{

    public function __construct()
    {
        parent::init();
    }

    public function index()
    {

        return view('welcome_message', $this->env_data);
    }

}
Just in case, here is my BaseController
Code:
<?php namespace App\Controllers;

class BaseController extends \CodeIgniter\Controller
{

    protected $env_data;
    protected $env_user;
    
    function __construct()
    {
        $this::init();
    }

    function init() {
        helper('html', 'url');
        $this->env_data = ['init'=>'yes'];
        $this->env_user = [];
    }

}
Thanks for your help.


RE: Redirect - InsiteFX - 12-09-2018

I have learned a lot by looking at  Lonnie's sample code like his Myth Auth etc;

You can find them here.

Lonnie Ezell - GitHub


RE: Redirect - Inc33 - 04-25-2020

(12-06-2018, 12:56 PM)kilishan Wrote: The redirect method returns a RedirectResponse class in CI4, which you must return from your methods. In Home2 it's doing parent::init(), but not returning the RedirectResponse if it is returned.

A second issue you'll run into, though, is that redirects cannot be returned from class constructors now, since we are using a class. You should instead look into using Controller Filters for those type of situations.
This needs to be made clear in the documentation, as I spent a bit of time as well to figure it out...
In the documentation it only mentions that it redirects a response, but doesn't state clearly how you need to handle that response.