Welcome Guest, Not a member yet? Register   Sign In
Redirect confusion
#1

HI there.

I am somewhat confused as to how to move from one controller to another. It would appear that the only way to do it is to use a redirect call like this - this is inside a controller....

Code:
class Welcome extends CI_Controller {

function __construct()
{

parent::__construct();

$this->load->library('ion_auth');
}

public function index()
{

if (!$this->ion_auth->logged_in())
{
redirect('auth/login', 'refresh');
}

$this->load->view('welcome_message');
}

Is this correct? As I understand it, this switches program flow from the Welcome Controller to the login method on the Auth controller. And inside the Auth controller, I could do something like :

Code:
if (!$this->ion_auth->logged_in())
{
// redirect them to the login page
redirect('auth/login', 'refresh');
}
else
{
redirect('welcome/index', 'refresh');
}

JMB
Reply
#2

@MightBeABitLate,

it looks right to me. Documentation ( https://codeigniter.com/user_guide/helpe...t#redirect )
Reply
#3

Thanks php-rocs.

This seems the way to effectively call a method in another controller - but of course it then transfers control TO that second controller.

JMB
Reply
#4

If it's login related, redirecting user to specific login related URL is not that bad, is it?

You can save some time and effort by doing user auth in construct method:
PHP Code:
function __construct()
{
    
parent::__construct();
    
$this->load->library('ion_auth');
    if (!
$this->ion_auth->logged_in()) {
        
redirect('auth/login''refresh');
    }
}

public function 
index()
{
    echo 
'Only registered users will see this';


You could also add a little referrer redirect, so you save current URL before redirecting to login page controller, and on successful login you redirect visitor to that saved URL, instead of default welcome page URL.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB