Welcome Guest, Not a member yet? Register   Sign In
Variable in MY_Controller
#1

[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')) ?
#2

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

[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();
#4

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

[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?
#6

[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.
#7

[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
#8

[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
#9

[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.
#10

[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




Theme © iAndrew 2016 - Forum software by © MyBB