Alok Mali
Alok Mali

Reputation: 2881

How to access model in constants.php in Codeigniter?

I want to access a function on application/config/constants.php, which is written in one of my CodeIgniter model. Is it possible to do it?

Upvotes: 0

Views: 292

Answers (1)

Alex
Alex

Reputation: 9265

There is no reason to change a constant with a model function or any function for that matter. Constants are meant to be static and strict definitions. Like application paths, version numbers, .etc.

They are similar to a variable except that they can never be changed.

If you wish to have a variable that is a superglobal but needs to be dynamic in some respect you can create a /application/core/MY_Controller.php

class MY_Controller extends CI_Controller {

     public $someglobalvar;

     public function __construct() {
          parent::__construct();
          $this->load->model('somemodel');
          $this->someglobalvar = $this->somemodel->get_var();
     }

}

and have your controllers extend it instead of CI_Controller

(application/controllers/Some_controller.php)

class Some_controller extends MY_Controller {

     public function index() {

          var_dump($this->someglobalvar); 

     }

}

Upvotes: 2

Related Questions