Where can I put the constant file for Codeigniter (PHP)?

I have a list of constants (I use them as an enumeration), some of them define operators, and some only global variables.

Where can I put them in an MVC structure, so I can use them both for my model and for my controller, which should reference it?

I would prefer not to embed it in config / constants.php, as they should not be called except for this model and the controllers that use it.

Edit 1: Clarification

To be more specific, I have my model message_model, and I have a bunch of constants that I need that are stored in message_model_constants.php. Where should I put message_model_constants.phpand there is a way to automatically turn it on with a controller that loads message_modelwhen message_modelit is not (and I do not want it to be) automatically loaded.

Edit 2:

I really do not want the constants to be loaded automatically, unless I use the model

+5
source share
5 answers

Extending Brennan Novak's answer, you can simplify your code by loading the configuration file into the model constructor. Thus, you only need to load the model in the controllers, and everything else will be done automatically.

Model

class Message_model extends Model {

    function __construct()
    {
        parent::Model();
        $this->load->config('message_model_constants');
    }
...
}

class Some_controller extends Controller {

    function __construct()
    {
        parent::Controller();
        $this->load->model('message_model');
    }
...
}

, application/config/message_model_constants.php

+2

application/config/constants.php , Model-View-Controller CI, "" ""

, php my_constants_helper.php.

__construct

$this->load->helper('my_constants');

=)

+4

. , :

application/controllers/messages.php

:

$this->config->load('messages');

,

function __construct() { 
   $this->config->load('messages');
   $this->load->model('message_model');
}

. , :

$this->config->item('item name')

$; , .

, "", , .

+3

config/config.php config/application.php, , .

$this->config->load('application'); // or autoload this - $autoload['config'] = array('application');
$this->config->item('item name');
+1

Message_Model? self::ConstantName Message_Model::ConstantName . .

0

All Articles