Codeigniter: user defined helper function does not

2019-05-30 10:14发布

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*

5条回答
我只想做你的唯一
2楼-- · 2019-05-30 10:41

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-30 10:41

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-05-30 10:42

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

查看更多
forever°为你锁心
5楼-- · 2019-05-30 10:49

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_';
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-05-30 10:51

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

查看更多
登录 后发表回答