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).

- 63,011
- 101
- 250
- 382
-
What is wrong with `APPPATH/classes/` as a place for a Utility class, e.g.? Did you already look into the [autoloading page on the docu](http://kohanaframework.org/3.3/guide/kohana/autoloading)? – kero Feb 22 '14 at 11:28
5 Answers
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.

- 56
- 1
-
Note that if you place it in a module, you can use the init.php file to always load it. Otherwise, place it in the bootstrap – Ikke Feb 25 '14 at 08:09
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' );

- 3
- 7
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.

- 342
- 2
- 10
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.

- 772
- 2
- 11
- 28