I'm new to code igniter. I'm going to create some generic functions like random_string($length), row_color($evenStyle, $oddStyle) etc...
Where do I put these functions such that they are accessible to my controller and view files?
I'm new to code igniter. I'm going to create some generic functions like random_string($length), row_color($evenStyle, $oddStyle) etc...
Where do I put these functions such that they are accessible to my controller and view files?
Coomer is right, however, put it where you benefit from it the most!
Libraries, Helpers or plugins. It seems plugins is for you.
random_string() is already available in the string_helper.
$this->load->helper('string');
echo random_string();
row_color() can be achieved with alternator() also in the string helper:
$this->load->helper('string');
for ($i = 0; $i < 10; $i++)
{
echo alternator('string one', 'string two');
}
In general, custom helpers are a good place to put functions like this, but it is worth checking the user guide first to make sure you aren't duplicating functionality.
Remember you can avoid writing $this->load->helper('string') everywhere by autoloading helpers in /system/application/config/autoload.php:
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('string');