views:

66

answers:

4

Hello,

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.

+1  A: 

You should place them in Controller, in controller you can put in even your own custom functions.

Sarfraz
It sounds really messy, wouldn't it be better to place a directory named `PHP` outside, next to the `system` and `javascript` directories?
thedp
@thedp: It is not that you can not put your own functions in the controller, even if you read the CI docs, it says that you can put in your functions there. However, if you feel it messy, you put it elsewhere you think appropriate.
Sarfraz
+1  A: 

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.

Justin Ethier
I'm looking to place PHP functions with general and global functionality that all of my application's features can use.So I'm having trouble deciding on the place: module, plugin, helper, external directory.
thedp
It sounds like you should use either a helper (if these are truly global functions) or a model (if the functions are reading/writing/modifying/etc a specific "type" of data on the file system).
Justin Ethier
+2  A: 

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.

Phil Sturgeon
And the ones that do?
thedp
If you require data interaction you should place the code in a model.
Justin Ethier
Helper = Group of functions for little stuff. Model = Methods for data interaction.Library = Group of functions for big stuff, that needs to share logic, properties, etc. i.e a class.
Phil Sturgeon
+1  A: 

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'.

micflan