• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect

#1
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!
Reply

#2
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.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

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

#4
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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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