CodeIgniter Forums
Variable in MY_Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Variable in MY_Controller (/showthread.php?tid=6571)

Pages: 1 2


Variable in MY_Controller - El Forum - 03-04-2008

[eluser]Kemik[/eluser]
Hello,

Is there anyway to create a variable (e.g. $user) in MY_Controller so I can use it in the views?

For example,

In my app I extend the controller to test for if the user is logged in, if not they are redirected.

Code:
class User_Controller extends Controller {

    function User_Controller() {
        parent::Controller();
        
        if (!logged_in()) {
            redirect('user/login');
            return;
        } else {
            // Insert variable here
        }
    }
}

How would I write out the variable so it can be passed to the view? Just a normal $data['user'] = array('user_id' => $this->session->userdata('user_id'), 'username' => $this->session->userdata('username')) ?


Variable in MY_Controller - El Forum - 03-04-2008

[eluser]adamp1[/eluser]
It would have to be a global variable, then you could just call.

Global
Code:
global $variable = "Something";

Think that would work.


Variable in MY_Controller - El Forum - 03-04-2008

[eluser]Derek Jones[/eluser]
$this->load->vars()

Edit-add: I believe you'll need to use get_instance() in your constructor:

Code:
$this->CI =& get_instance();
$this->CI->load->vars();



Variable in MY_Controller - El Forum - 03-04-2008

[eluser]adamp1[/eluser]
That's the better way, I forgotten about that command.


Variable in MY_Controller - El Forum - 03-04-2008

[eluser]Kemik[/eluser]
So for $this->load->vars() I'd need to do the following?

MY_Controller
Code:
class Public_Controller extends Controller {

    function Public_Controller() {
        parent::Controller();
        
        $user = array('user_id' => $this->session->userdata('user_id'), 'username' => $this->session->userdata('username'));
    }
}

User Controller
Code:
$data['user'] = $this->load->vars($user);

View
Code:
echo $user['username'];

Or use $this->load-vars(); direct in the view file?


Variable in MY_Controller - El Forum - 03-04-2008

[eluser]adamp1[/eluser]
No you would do this
Code:
class Public_Controller extends Controller {

    function Public_Controller() {
        parent::Controller();
        
        $data['user'] = array('user_id' => $this->session->userdata('user_id'), 'username' => $this->session->userdata('username'));
        $this->load->vars($data);

    }
}

Then in the view you could just use $user. It does exactly the same as the second parameter in $this->load->view() but allows you to do it in a constructor.

Quote:This function takes an associative array as input and generates variables using the PHP extract function. This function produces the same result as using the second parameter of the $this->load->view() function above. The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.



Variable in MY_Controller - El Forum - 03-04-2008

[eluser]Kemik[/eluser]
Oh ok, I understand now thanks. Works great.

I did read the documentation Derek linked to but I misunderstood it. Opps :p


Variable in MY_Controller - El Forum - 03-04-2008

[eluser]Kemik[/eluser]
Just to add to this, how would I use the same variable ($data['user']) from MY_Controller in the controller itself so I can use it in SQL queries such as inserting news?

load->vars() passes it straight to the view so I cannot use the output from that in this case.

Thanks


Variable in MY_Controller - El Forum - 03-04-2008

[eluser]Derek Jones[/eluser]
Kemik, you'd need to make it a class property of the Controller and access it with $this->foo. View variables and controller object variables are different animals.


Variable in MY_Controller - El Forum - 03-05-2008

[eluser]webthink[/eluser]
Without being rude... It seems like cases like these where the OP is missing really fundamental parts of using CI or indeed OOP they might be better served by looking over the manual.
So in the interest of not being rude... RTFM Wink