views:

154

answers:

5

I have a list of constants (I'm using them as an enum), some are define statements, and some are just global variables.

Where am I suppose to put them in the MVC framework so I can use them for both my model and my controller that needs to reference it?

I'd rather not stick it into config/constants.php since they shouldn't be called except for by this model and the controllers that use it.

Edit 1: Clarification

To be more specific, I have my message_model model and it has a bunch of constants that I need that are stored in message_model_constants.php. Where should I put message_model_constants.php and is there a way to have it automatically included by the controller that loads message_model when message_model is not (and I don't want it to be) auto-loaded.

Edit 2:

I really don't want to have the constants auto-loaded except for when I use the model

+1  A: 

For global configs add the to the config/config.php or create a config/application.php and then load the config, then the item.

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

Dave
+3  A: 

You can choose to load a particular config file when you load a particular model in the controller. For instance in your file:

application/controllers/messages.php

You would use a line like this:

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

If you include it at the top of your controller like this

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

Then all of those constants will be available to all the functions and methods in the given controller. You then call each config constant like:

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

And you can name protected $variables; in the construct as well for shorter syntax.

If you are using these config constants and the message model in multiple different controllers you may want make a "Library" file that then loads both the config and the model and declares all variables there.

Brennan Novak
A: 

Have you considered just adding your constants to your Message_Model Class? You'll then reference them by self::ConstantName inside the Class and Message_Model::ConstantName outside the class. This would also prevent name space collision.

Thomas Montague
I don't think this works based on how Codeigniter loads Models.
wag2639
A: 

Go to

application/config/constants.php

and define your constant their and you can use your constants on Model-View-Controller of CI not include "Helper" and "Library"

but in your case I prefer you to create a php file that has your constants and rename it to something like my_constants_helper.php

in your model or controller __construct just $this->load->helper('my_constants');

And hooray you can access them =)

DominixZ
+1  A: 

extending Brennan Novak answer, you can simplify your code by loading your config file in the model constructor. That way, you only have to load the model in your controllers and everything else is done automatically.

Model

class Message_model extends Model {

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

Controller

class Some_controller extends Controller {

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

As already stated, your config files should be application/config/message_model_constants.php

Stolz