Welcome Guest, Not a member yet? Register   Sign In
Accessing a User Object From a Parent Controller
#1

[eluser]xtremer360[/eluser]
I'm trying to find out how to access the $user_data object from the Admin Controller to the Dashboard Controller.

Code:
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Admin_Controller extends CI_Controller
{
    public $data = array();
    public function __construct()
    {
        parent::__construct();
        $this -> load -> model('user_model', 'user');
        $user_id = $this -> session -> userdata('user_id');
        $user_data = $this -> user -> get($user_id);
        $this -> data['user_data'] = $user_data;
        $this -> template -> set_theme('saturn') -> set_layout('default', 'admin') -> set_partial('navigation', 'admin/partials/navigation') -> set_partial('header', 'admin/partials/header');
    }
}

Code:
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Dashboard extends Admin_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        if ($user_data -> role_id == 2)
        {
            $dashboard = 'admin_dashboard';
        }
        else
        {
            $dashboard = 'user_dashboard';
        }
        $this -> template -> build('admin/'.$dashboard, $this -> data);
    }
}
#2

[eluser]jonez[/eluser]
Code:
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Dashboard extends Admin_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        if ($this->data[ 'user_data' ]->role_id == 2)
        {
            $dashboard = 'admin_dashboard';
        }
        else
        {
            $dashboard = 'user_dashboard';
        }
        $this -> template -> build('admin/'.$dashboard, $this -> data);
    }
}
#3

[eluser]xtremer360[/eluser]
Great. Why is it you went that route instead of doing something with the $user_data variable? I'm asking so I can understand.
#4

[eluser]jonez[/eluser]
Since Dashboard extends Admin it has access to all it's public and protected variables/methods. In Admin you define a public variable $data, even though you don't see it in Dashboard it's still there because Dashboard includes everything in Admin plus it's new stuff.

As to the variable name I just modified your code so it will work. If you want it named something else, rename or add another public variable ($user_data) to Admin just like $data is defined. Then in your construct assign it to that and access it using $this-> in Dashboard just like you would in Admin.

Classes in PHP have 3 types of methods:

public - accessible for this class, extended classes, and the outside world
protected - accessible for this class, extended classes, not the outside world
private - accessible only for this class, not extended classes, not the outside world

http://www.php.net/manual/en/language.oop5.php
#5

[eluser]xtremer360[/eluser]
If I were to define it in the the class as a public variable, what if any, would I set it equal to or do I even need to?
#6

[eluser]jonez[/eluser]
If you do not want an outside function call to see it define it as protected. If you are only using it to store data for internal use it doesn't need to be public. It can be but it's better security to use protected.

If you aren't assigning a value in construct you would set it to an empty array or object depending on what you plan on doing with it. Or in your case you'd assign it to the a function call and that function's return value would set it's type.

Code:
class Admin_Controller extends CI_Controller {
protected $data;
protected $user_data;

public function __construct( ) {
  parent::__construct( );

  $this->data = array( ); //array
  $this->user_data = new stdClass( ); //object
}
}




Theme © iAndrew 2016 - Forum software by © MyBB