Welcome Guest, Not a member yet? Register   Sign In
Calling a controller from a different controller
#1

[eluser]Unknown[/eluser]
I'm just asking if this is possible.

Here's the situation:

I have 2 controllers: user.php and home.php

My user.php controller has 2 methods/functions: login() and loginSuccess()

to shorten things up, I would want my loginSuccess() method to access the home.php controller because whenever it successfully logs in, loginSuccess only loads the view, but does not access the home controller which makes the URI still on user/loginSuccess.


Problem is, I don't know how to do it since I don't know if you can load controllers like $this->load->controller('home').

Does anyone know how to do this? Or is it even possible accessing another controller from another controller? Thanks!
#2

[eluser]InsiteFX[/eluser]
Code:
redirect('home', 'refresh');

InsiteFX
#3

[eluser]Cristian Gilè[/eluser]
user controller
Code:
class User extends CI_Controller
{
    function login()
    {
        ...

        //load the login form

        ...
    }

    function loginSuccess()
    {
        ...

        //if login is success set a session var and redirect to home
        $this->session->set_userdata('login',TRUE);
        redirect('home');

        ...
    }

    ...
}

home controller


Code:
class Home extends CI_Controller
{
   function __construct()
   {
      parent::__construct();

      //if a not logged in user tries to access the home controller he is redirected to login form
      if( ! $this->session->userdata('login'))
          redirect('user/login');

   }

   function index()
   {
      //welcome home
   }

   ...
}

You need to load the url helper and the session library.


Cristian Gilè




Theme © iAndrew 2016 - Forum software by © MyBB