Codeigniter - Best Place to Declare Global Variable

CodeIgniter - best place to declare global variable

There is a file in /application/config called constants.php

I normally put all mine in there with a comment to easily see where they are:

/**
* Custom defines
*/
define('blah', 'hello mum!');
$myglobalvar = 'hey there';

After your index.php is loaded, it loads the /core/CodeIgniter.php file, which then in turn loads the common functions file /core/Common.php and then /application/constants.php so in the chain of things it's the forth file to be loaded.

How to use global variable in PHP Codeigniter

Don't use GLOBALS you can just use a private variable in your class.

  • Create the variable above your __construct function like private $er
  • In your __contruct function set the default value
  • Set and get in your public function using $this->er

Implemented in your code:

class Login extends CI_Controller {

private $er;

public function __construct() {
parent::__construct();
$this->er = FALSE;
}

public function index() {
$data['er']= $this->er;
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}

public function validate_credentials() {
$this->load->model('user_model');
$query = $this->user_model->validate();
if ($query) {
$data = array(
'username' => $this->input->post('username'),
);
$this->session->set_userdata($data);
redirect('pmpBulletin/members_area');
//die(here);
} else {
$this->er = TRUE;
$this->index();
}
}
}

CodeIgniter - best place to declare global variable

There is a file in /application/config called constants.php

I normally put all mine in there with a comment to easily see where they are:

/**
* Custom defines
*/
define('blah', 'hello mum!');
$myglobalvar = 'hey there';

After your index.php is loaded, it loads the /core/CodeIgniter.php file, which then in turn loads the common functions file /core/Common.php and then /application/constants.php so in the chain of things it's the forth file to be loaded.

how to define Global variable in codeigniter?

file location - Application -> Config -> constants.php here you can define global variable

define('BASE_PATH',"/home/example/public_html/");
define('UPLOAD_PATH',BASE_PATH . 'uploads/');

//this is your global variable, can use anywhere in application
echo $UPLOAD_PATH;

How to define a global variable(value) in codeIgniter

Create A core controller, since your process requires logical operations then you need a method for that.

application/core/MY_Controller.php

class MY_Controller Extends CI_Controller
{

protected $default_theme = 'theme';

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

public function get_theme()
{
//your code for selecting the current theme selected from
//the database
$theme_from_db = '';

return $theme_from_db == NULL ? $this->default_theme : $theme_from_db;
}
}

Your Controller must extend MY_Controller

application/controller/view.php

class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}

Codeigniter global variable in view

I noticed that you can access variables in views without passing them if you declare them in the controller with $this->. Probably because they default to public visibility.

 public function __construct() {
parent::__construct();
// $data['count_student'] = $this->m_data->allStudent(); // count all students
//
$this->count_all_the_students = $this->m_data->allStudents();

}

public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}

And then in the view you can use $this->count_all_the_students without putting it in the $data array.

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<?php
echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';

In CodeIgniter, where should I declare my global variables?

You may utilise config file (system/application/config/config.php) to set configuration related variables.

Or use constant file (system/application/config/constants.php) to store site preference constants.

How to declare global variables in php codeigniter 3.1.9?

You can add your Global Variables in Constant File

File PATH: \application\config\constants.php

Or

You can add your Global Variables in Config File

File PATH: \application\config\config.php

define('ThemeColor', 'blue');//key and value

Now ThemeColor is your global variable you can use it in entire MVC pages, No need to write '$' before variable just use it as you wrote in define

Codeigniter create and access global variable

If your intention is to provide a default \description of how to handle the calculation then what you have done is perfectly acceptable. I would suggest you access the variable in this way.

$calc_method = $this->config->item('expenses_calculation');

The advantage to this is that item('expenses_calculation') will return NULL if the item doesn't exist.

But if $this->config->config['expenses_calculation']; doesn't exist an "Undefined index" PHP error will be thrown.



Related Topics



Leave a reply



Submit