Reputation: 415
I have a simple control panel controller that is responsible for extending the backend controller as well as building the control panel dashboard for the logged in user. There are two possible dashboards to load depending on the logged in user's role. If the user has a role of 4 then it loads the admin dashboard if it is anything else then it loads the regular user dashboard. If you notice below it requests a property from the $user_data
object called role_id. This object is set in the backend controller. I did this because this object is used across most of my controllers.
When I load the control panel controller I receive an undefined variable $user_data
as well as trying to get property of non object. Can I not set the variable inside of the backend controller and use it in the control panel as I am doing.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Control_panel extends Backend_Controller
{
/**
* Control_panel::__construct()
*
* Load the parent construct and any additional models, helper, libraries available.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Control_panel::index()
*
* Loads the dashboard that pertains to user's role
* @return void
*/
public function index()
{
$this->template
->title('Control Panel');
if ($user_data->role_id == 4)
{
$this->template
->build('admin_dashboard_view');
}
else
{
$this->template
->build('user_dashboard_view');
}
}
}
Backend Controller
$user_data = $this->user->get_by('user_id', $this->session->userdata('user_id'));
$character_data = $this->character->get_many_by('owned_by', $this->session->userdata
('user_id'));
$user_data->characters = $character_data;
$this->template
->set_theme('smashing')
->set_layout('control_panel_view')
->set_partial('header', 'partials/header')
->set_partial('sidebar','partials/sidebar')
->set_partial('footer', 'partials/footer')
->set('user_data', $user_data);
Upvotes: 1
Views: 60
Reputation: 36947
If I had to guess I am going to assume $user_data
is not a class level variable, and that it is contained within the function(){}
itself. Which if this is the case then after that function has complete that concept of garbage collection kills that variable, as it only pertains to the function as far as the interpater is concerned.
What you need is in your backend controller to do something like
class BackendController extends Whatever {
private $_user_data = null;
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function myBackend()
{
$this->_user_data = $this->user->get_by('user_id', $this->session->userdata('user_id'));
$character_data = $this->character->get_many_by('owned_by', $this->session->userdata
('user_id'));
$user_data->characters = $character_data;
$this->template
->set_theme('smashing')
->set_layout('control_panel_view')
->set_partial('header', 'partials/header')
->set_partial('sidebar','partials/sidebar')
->set_partial('footer', 'partials/footer')
->set('user_data', $user_data);
}
}
then in your front end controller
/**
* Control_panel::__construct()
*
* Load the parent construct and any additional models, helper, libraries available.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Control_panel::index()
*
* Loads the dashboard that pertains to user's role
* @return void
*/
public function index()
{
$user_data = $this->_user_data;
$this->template
->title('Control Panel');
if ($user_data->role_id == 4)
{
$this->template
->build('admin_dashboard_view');
}
else
{
$this->template
->build('user_dashboard_view');
}
}
}
Upvotes: 1