Question

Say I want a constant, a function or a class to be available in all my models, views and controllers code (within a particular web site (application)). Where can I put them? I don't mind importing them explicitly in every file I need them in but I don't know how (simple require_once does not seem to work).

No correct solution

OTHER TIPS

You can put them in the vendor folder (in application/vendor or modules/MOD/vendor). Then you can load it like this:

require Kohana::find_file('vendor', 'folder/file','ext');

You can read up more on this in the user guide

Now, it should be stated you should in general not use functions or globals.

I declare Constants in Bootstrap.php and create my own Helpers for general functions under application/classes/Helpers.

If you need to integrate a third party library into Kohana or want to make code available to other Kohana users consider creating a module instead.

You can define all your constants in new php file and place it in the application/classes directory. In your Template controller or in the main controller like Welcome or Website, just place the code in the __constructor()

require_once Kohana::find_file( 'classes', 'filename' );

I suggest you to put your constant variables in the bootstrap.php file because this is loaded by the framework before every request.

You can simply put your classes in the root of the classes directory, the framework will find.

This worked well for me...

In application/config/constants.php:

define('SOME_COOL_CONSTANT', 'foo');
return array();

In index.php:

Kohana::$config->load('constants');

SOME_COOL_CONSTANT should then be available globally.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top