Codeigniter: user defined helper function does not

2019-05-30 09:54发布

问题:

I made a custom helper extending the system string_helper.php.

I placed it in my /application/helpers directory, called it MY_string_helper.php as required, unit-tested its functions.

Now, when I try to call one of its functions from a model, it does not work.

The functions in the default string helper work, instead. It looks like my extension is not loaded for some reasons.

Thanks a lot, and happy holidays.


Edit: even funnier. I saved the file as categories_helper.php in the system/helpers directory, and when I try to load it within a model i got the following response: *Unable to load the requested file: helpers/categories_helper.php*

回答1:

I had the same problem. I came from windows OS development of my codeigniter project, then shifted to Ubuntu, but somehow it still didn't load my helpers.

I found that changing the naming convention to lowercase solves the problem. Don't follow what's written in the doc where you are given a prefix to insert, especially if it's in uppercase. Make use of lowercase.



回答2:

The Codeigniter User Guide for helpers describes that helpers are available to controllers and views only. It does not explicitly mention that helper functions work in models.

CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.



回答3:

Linux is case sensitive, Windows no. So, if you are development in OS Windows, before upload your system in a Linux hosting foe example, you need have the names of the files in lowcase.

For example:

In Windows: application/helpers/MY_functions_helpers.php
In Linux: application/helpers/my_functions_helpers.php



回答4:

mmm, i'm not sure it will work for helpers, but have you tried to load the helper in this way?

$CI = & get_instance();

$CI->load->helper('string');



回答5:

I faced a similar kind of situation. My development environment was WINDOWS and it worked fine as described in the DOCs i.e. MY_string_helper.php

On the production environment of UNIX, it gave error:

Unable to load the requested file: helpers/my_form_helper.php

Changing the file name to lower case resolved this error BUT the helper was not actually loaded.

After a lot of time waste, by hit and trail it worked [ it is un-documented ]

In the autoload.php, just include the mention the extended helper before the default helper:

$autoload['helper'] = array('my_string', 'string');

Also, the prefix has to capital, it did not work with lower case prefix:

$config['subclass_prefix'] = 'MY_';