tags:

views:

127

answers:

4

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?

A: 

It sounds like a helper is a good place for those.

http://codeigniter.com/user_guide/general/helpers.html

Coomer
A: 

Coomer is right, however, put it where you benefit from it the most!

Industrial
A: 

Libraries, Helpers or plugins. It seems plugins is for you.

http://codeigniter.com/user_guide/general/plugins.html

http://codeigniter.com/user_guide/general/helpers.html

shin
A: 

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');
Phil Sturgeon