简体   繁体   中英

CodeIgniter: placing PHP functions?

I'm still quite new to CodeIgniter and I was wondering, where should I place my PHP functions that has nothing to do with Controllers and Views, for example, a function that access a local file.

Thank you.

Do not use plugins as they are removed from CI 2.0 and you will have to convert them.

A group of functions that do not require data interaction should be placed in a helper.

Put loose functions into helpers. Group similar functions together into a helper and give it a meaningful file-name. Once the helper is loaded the functions can be used as though it were a require_once() or include.

If you have a class which has ostensibly "nothing to do" with Codeigniter, these can usually be converted into libraries with minimal or no effort.

Plugins are being taken out in CI 2.0, as Phil Sturgeon said, in favour of helpers and libraries. Which is a good thing, I think. Never had much need for 'plugins'.

您应该将它们放置在Controller中,在Controller中甚至可以放置自己的自定义函数。

There are plugins and helpers directories where you can place files that include 'global' functions that are shared across your entire application.

That said, you should think before doing this, it may well make more sense to place the file in a model, if you are working with data stored on the file system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM