简体   繁体   English

CodeIgniter:放置PHP函数吗?

[英]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. 我对CodeIgniter还是很陌生,我想知道应该在哪里放置与Controllers和Views无关的PHP函数,例如访问本地文件的函数。

Thank you. 谢谢。

Do not use plugins as they are removed from CI 2.0 and you will have to convert them. 不要使用插件,因为它们已从CI 2.0中删除,您将不得不对其进行转换。

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. 加载帮助程序后,就可以像使用require_once()或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. 如果您的类表面上与Codeigniter无关,那么通常可以轻松地将它们转换为库。

Plugins are being taken out in CI 2.0, as Phil Sturgeon said, in favour of helpers and libraries. 正如Phil Sturgeon所说,在CI 2.0中正在使用插件,以支持助手和库。 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. 就是说,您应该考虑一下,如果要处理存储在文件系统中的数据,那么在模型中放置文件可能更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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