Ivan Gozali
Ivan Gozali

Reputation: 2249

Using CakePHP's App::import() Function to Import Constant.php File

Basically, I have a file called Constants.php, which is located in /app/Lib/Constants.php, and I want to use it in one of my Model classes (and possibly other classes that I've built). However, I can't seem to figure out the correct syntax to do this (and CakePHP's documentation on App::import() is hard for me to understand).

How do I properly do this? Or is there another better convention of defining user-defined constants in CakePHP applications (besides using the Configure class)?

Upvotes: 1

Views: 3443

Answers (2)

Predominant
Predominant

Reputation: 1460

If the Constants.php file is one in which you are defining configuration variables for your application, I would suggest using the Configure class.

Place the Constants.php file into the Config/ directory of your application.

In your Config/bootstrap.php you load the constants with the following line:

Configure::load('Constants');

Now, anywhere in your application you can read configuration information with the following:

Configure::read('Constants.Something');

Note, the Configure setup requires a particular format for your configuration variables. Use the following in your Constants.php file:

$config = array('Constants' => array(
    'Something' => 1234,
    'Foo' => 'Bar',
));

This is the recommended way to do configuration information, loaded in and made available to your entire application.

Upvotes: 3

Hoff
Hoff

Reputation: 1772

The manual is a better place to look as it explains it better :) All you have to do is this:

App::import('Lib', 'Constants');

Upvotes: 0

Related Questions