How to make a global variable that always changing?
I want to create something like protected $global_data; in controller Global_data.php that can be called by a lot of controller, and return variable $global_data value to whenever controller that has $this->load->library('../controllers/Global_data').
But when I tried to call it, it gives me this error Unable to locate the specified class: Session.php, so I think CodeIgniter 3.1.8 not allowed me to do this.
So how to achieve what I'm looking for? do I need to put it on model instead, library file or is there another way to do it?
Thank you.
Here is Global_data.php content
protected $global_data;
public function __construct()
{
parent::__construct();
$this->global_data = array(
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
);
}
Can_be_anything_controller.php content
class Can_be_anything_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('../controllers/Global_data');
}
public function index()
{
$data = $this->global_data;
$data['page_title'] = 'Dashboard';
$data['page_directory'] = 'pages/dashboard';
$this->load->view('template', $data);
}
}
You may create a library for that in libraries directory
Global_data.php file
class Global_data{
public $global_data;
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function common_data()
{
$this->global_data= array(
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
);
return $this->global_data;
}
public function any_method(){
$query = $this->CI->db->get('table_name');
}
}
Now you can load it in any controller like
$this->load->library('Global_data')
Then use data
$data = $this->Global_data->common_data();
Also you may use HMVC model to use any method in any controller
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
Make a MY_Controller in application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public $global_data;
public function __construct() {
parent::__construct();
$this->global_data = 'whateveryouwant';
}
public function somemethod() {
return '123';
}
}
then any other controllers in your application/controllers that need to access global_data should extend it like so:
class Somecontroller extends MY_Controller {
public function index() {
echo $this->global_data;
echo $this->somemethod(); // works with methods too
}
}
If you need to run more complex code just put everything into a library or model and autoload it. All public methods and properties are globally available. HMVC seems overkill for what you want.
This kind of data can also be handled very nicely by config files.
/application/config/global_data.php
<php
$config['foo'] = "some foo";
$config['bar'] = 42;
$config['baz'] = array('one', 'two', 'three');
In a controller load the config file with
$this->config->load('global_data');
The access the items using
echo $this->config->item('foo');
echo $this->config->item('bar') * 2; //outputs 84
$data = $this->config->item('baz');
Config documentation
Related
Am trying to load view file in application/core/MY_Controller.php but its giving as follows.
Message: Undefined property: Edustaticlanding::$load
Filename: core/MY_Controller.php
Function is as follows in MY_Controller.php
function menu(){
$arrr = array();
return $arrdata['menu'] = $this->load->view('menu',$arrr,true);
}
And am calling this function in controller(Edustaticlanding.php) as follows.
function __construct(){
$this->menucontent = $this->menu();
print_r($this->menucontent); die;
}
Pls correct me.. where its going wrong.. thanks
On application/core/MY_Controller.php
Add public $data = array() like below
<?php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
$this->data['menu'] = $this->menu();
}
public function menu(){
return $this->load->view('menu', NULL, TRUE);
}
}
On the controller then once add public $data = array(); you can access the menu on view
You have to use $this->data now on controller
<?php
class Example extends MY_Controller {
public $data = array();
public function index() {
$this->data['title'] = 'Welcome to Codeigniter';
$this->load->view('example', $this->data);
}
}
On the example view now you can echo
<?php echo $menu;?>
Add extends CI_Controller to your core controller like following codes:
class MY_Controller extends CI_Controller {
Please check below mentioned solution. You need to call parent constructor first. So it will load all basic configurations.
First extent CI_Controller class and call parent constructor like describe below
class MY_Controller extends CI_Controller {
public function __construct{
parent::__construct();
}
}
Please let me if it not works.
I have two layout templates in my project and always loading first template. How to load second template also.
In main Usercontroller how to differentiate them. They are,
Learncontroller.php
class Learncontroller extends Usercontroller{
public function __construct(){
parent::__construct();
$this->load->model("Usermodel","",true);
}
public function index(){
$id=$this->session->userdata('cp_userid');
$menuActive= "learning";
$data['menuActive'] = $menuActive;
$userdetails=$this->Usermodel->getuserdetails($id);
$data['userdetails']=$userdetails;
$result=$this->Usermodel->currentlearningcourses($id);
$data['details']=$result;
$data['content']=$this->load->view("user/currentlearningcourses",$data,true);
$data['title']='My Courses';
//$this->load->view("user/layout",$data);
$headerContent = $this->load->view("user/layout",$data,true);
$this->render($headerContent);
}
}
Unitcontroller.php
class Unitcontroller extends Usercontroller{
public function __construct(){
parent::__construct();
$this->load->model("Usermodel","",true);
}
public function courselearn($id){
$r=$this->Usermodel->getsectiontopic($id);
$data['topicId']=$id;
$data['topicQ']=$r;
$data['video']=$this->input->post("id");
$data['id']=$id;
$data['content']=$this->load->view("user/unit_content",$data, true);
$courselearnheaderContent = $this->load->view("user/courselearn_layout",$data,true);
$this->render($courselearnheaderContent);
}
}
Learncontroller and Unitcontroller has separate layouts but always loading protected $layout = 'user/layout';. How to load this 'user/courselearn_layout'; when control comes from Unitcontroller.
Usercontroller.php
protected $layout = 'user/layout';
protected $courselearn_layout = 'user/courselearn_layout';
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->load->view($this->layout);
}
Always loading first page. How to make load the second page also please help me.
I am creating a master controller so that every other controllers on my app extend the master (MY_Controller).
My problem is how to make loading a model more abstract, let me show you the code to better explain.
class MY_Controller extends CI_Controller
{
protected $model;
function __construct()
{
parent::__construct();
}
function get($order_by)
{
$this->load->model($this->model);
$query = $this->$this->model->get($order_by);
return $query;
}
}
I declare a protected variable $model in the master controller so that on the extending controller i can asign it a value:
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
$this->model = "home_model";
}
public function index()
{
$test = $this->get('id');
}
}
The problem is that on MY_Controller i cant load the model
$this->$this->model->get();
i get the following error: Message: Object of class Home could not be converted to string
any help will be appreciated , thank you !
Why do not save the value you assign to protected global $model variable in a variable inside the function as such:
class MY_Controller extends CI_Controller{
protected $model;
function __construct(){
parent::__construct();
}
function get($order_by){
$_model = $this->model // I added this line.
$this->load->model($_model); // I modified this line.
$query = $this->$_model->get($order_by); // I modified this line.
return $query;
}
}
The $this->$this->model->get() is your problem. If you want to use an object property ($this->model) inside a chain like this, you need to wrap it in braces: $this->{$this->model}->get().
Hello guys I have a problem with calling array which is declared in MY_Controller and want to use in Welcome controller. I'm new with ci so I think it's a simple problem or I has forgot something to load in config... (I'm using ci 2.2)
MY_Controller :
class MY_Controller extends CI_Controller {
public $data = array();
function __construct() {
parent::__construct();
$data['test'] = 'Hello World';
}
}
Welcome controller >
class Welcome extends MY_Controller {
public function index()
{
var_dump($this->data);
$this->load->view('welcome_message');
}
}
and the result is :
array (size=0)
empty
Why is my array empty, why not "Hello world"?
Change your MY_Controller constructor to below:
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->data = array('test' => 'Hello World');
}
}
No need of this:
public $data = array();
Explanation:
To access the members of the same or parent class you have to use the keyword $this.
Example:
1) To access/declare a variable:
$this->variable_name;
2) To access a method/function:
$this->function_name();
I have extended my CI_Controller like this:
// base extend
class MY_Controller extends CI_Controller {
public $CI = array();
public function __construct() {
parent::__construct();
$this->CI = & get_instance();
}
public function isUser(){
// for example
}
}
// admin extended
class MY_AdminController extends MY_Controller {
public $admin = array();
public function __construct() {
parent::__construct();
$this->CI->lang->load('admin');
$this->admin['lang'] = $this->CI->lang->line('admin');
$this->CI->load->vars($this->admin);
}
public function isAdmin(){
//for example
}
}
// extends for modules
class MY_AdminModuleController extends MY_AdminController {
public function __construct() {
parent::__construct();
$this->CI->load->view('_header');
}
public function isAllowedModule(){
// example
}
public function pseudoDestruct(){
$this->CI->load->view('_footer');
}
}
So it works fine. But I try to hook post_controllerevent and add my MY_AdminModuleController->pseudoDestruct(), so I enabled hooks in config.php and added next lines to hooks:
$hook['post_controller'] = array(
'class' => 'MY_AdminModuleController',
'function' => 'pseudoDestruct',
'filename' => 'MY_Controller.php',
'filepath' => 'core'
);
But I got a problem at loading lang-file in MY_AdminController's constructor. It returns null when called from hook (true when I use it normaly) and I have Notice about undefined index at frontend. No, I don't want to disable notices, I want to fix the problem. Also I have config loadings in MY_AdminController's constructor and them loading good.
You can't do that, not in that particular way at least. CodeIgniter is designed to only have one controller instance, while the hooks create new instances and your lang files are not loaded in that new instance.
Also, you don't need to call get_instance() from your controller class - the class IS what get_instance() returns.
Anyway, you can declare a regular function to use as a hook, there's no problem putting it in your MY_Controller.php file as well:
function pseudo_destruct()
{
get_instance()->load->view('_footer');
}
Then use this hook:
$hook['post_controller'] = array(
'function' => 'pseudo_destruct',
'filename' => 'MY_Controller.php',
'filepath' => 'core'
);