开发者

Where to put generic functions in Code Igniter?

I'm new to code igniter. I'm going to create some generic functions like random_string($length), row_co开发者_如何学Clor($evenStyle, $oddStyle) etc...

Where do I put these functions such that they are accessible to my controller and view files?


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

https://www.codeigniter.com/user_guide/general/helpers.html


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


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


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

http://cimple.org/user_guide/general/plugins.html

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜