Codeigniter: user defined helper function does not load

浪尽此生 提交于 2019-12-23 03:59:09

问题


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



回答6:


you just have to change your helper function name with your prefix like found in application/config/config.php

after helper renamed make sure it contains small letters only

then change autoload file with the function name that you renamed,

main problem because of OS like your Hosting server is linux and you develope with windows so this problem rise



来源:https://stackoverflow.com/questions/4529327/codeigniter-user-defined-helper-function-does-not-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!