CodeIgniter Forums

Full Version: How to set siteside constants
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]omed habib[/eluser]
Hi,

Where exactly can I define constants that can be used through the entire site (i.e. model, view and controller)?

I have constants I want to define that are server specific (for my various development environments and the live site) that I want to define to be used throughout the site.

Thanks!
Omed

El Forum

[eluser]woopsicle[/eluser]
config.php works for me.

El Forum

[eluser]omed habib[/eluser]
I tried that... I can't figure out how exactly to set the constant. For example, doing this:

define("MAXSIZE", 100);

The constant 'MAXSIZE' isnt accessible. Any ideas?

El Forum

[eluser]woopsicle[/eluser]
mmm strange.

an example of how i use this is below:

config.php
Code:
...
define('HOME_PATH', '/home/mydomain/mydomain.local/');

my_controller.php
Code:
...
$myVar = HOME_PATH;

This works perfectly for me.. but if you are having problems I have no idea Sad could be a server problems.. but it is strange.

El Forum

[eluser]esra[/eluser]
I use a lot of constants in helper and plugin files and sometimes at the head of some controller files. I modified index.php to require a separate defines file just after APPPATH is defined in index.php.

Code:
/*
|---------------------------------------------------------------
| DEFINE APPLICATION CONSTANTS
|---------------------------------------------------------------
|
| EXT  - The file extension.  Typically ".php"
| FCPATH - The full server path to THIS file
| SELF  - The name of THIS file (typically "index.php)
| BASEPATH - The full server path to the "system" folder
| APPPATH - The full server path to the "application" folder
|
*/
define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));
define('FCPATH', __FILE__);
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
define('BASEPATH', $system_folder.'/');

if (is_dir($application_folder))
{
define('APPPATH', $application_folder.'/');
}
else
{
if ($application_folder == '')
{
  $application_folder = 'applications';
}

define('APPPATH', $application_folder.'/');
}

// Include additional constants
$defines_file = 'includes/defines.php';
if (file_exists($defines_file))
{
require_once($defines_file);
}

Then you can add whatever constants you need quickly. In the above, I store the defines.php under includes/.

El Forum

[eluser]omed habib[/eluser]
This was awesome... thanks guys... your posts were of definite help!

Coming from an (officially) former user of Symfomy, PHP Fusebox and CakePHP user, I have to say, I LOVE this framework!

El Forum

[eluser]maomaode[/eluser]
Is it possible to define a constants just for some models, say, in case of a blog system, i want to some constants just for the post model, so i have a post_constants.php, if it's a comment model, i have a comment_constants.php...

El Forum

[eluser]Unknown[/eluser]
Fantastic Esra! This was just what I was looking for. Clean, functional, and easy to understand for the next developer.

Thanks!

El Forum

[eluser]Milos Dakic[/eluser]
Couldn't the constants.php file in the application/config folder be used for this or am I missing the point?

El Forum

[eluser]TheFuzzy0ne[/eluser]
[quote author="Milos Dakic" date="1236056663"]Couldn't the constants.php file in the application/config folder be used for this or am I missing the point?[/quote]

Sure, but that's just too easy! Tongue
Pages: 1 2