Welcome Guest, Not a member yet? Register   Sign In
Custom functions. Where to put them?
#1

[eluser]callumd[/eluser]
Hi There,

I have some custom functions that I want my CodeIgniter app to use. Where should I put them?

Should I create a new folder and put them in there? If so, what's the best way to "include" them in the controllers/classes that need them?

CodeIgniter doesn't seem to enforce any way of doing it.. so perhaps what I'm really asking for is, what is considered "best practise"?

Thanks.
#2

[eluser]lennierb5[/eluser]
I'm not sure if this is the 'best' way but for any custom functions I created my own helper file placed in application/helpers which I autoload in the config/autoload.php file since I use these functions in almost all of my application.
#3

[eluser]callumd[/eluser]
Great, thanks.

What about functions pertaining to business logic?

Eg.. let's say the app has a field where the user enters in the amount of water they used on a given day. The app needs to check if this figure is below 200 liters, and a couple of other small checks. Pre framework days, I'd just write a function called isWaterUsageAcceptable() which would return true or false.

Doing it proper MVC style with CodeIgniter, where would such functions belong?
#4

[eluser]Cristian Gilè[/eluser]
It seems a validation rule. If you need it when you run form validation put it in your extended form_validation class, otherwise use an helper or a library.

Cristian Gilè
#5

[eluser]mdvaldosta[/eluser]
[quote author="callumd" date="1294827181"]Great, thanks.

What about functions pertaining to business logic?

Eg.. let's say the app has a field where the user enters in the amount of water they used on a given day. The app needs to check if this figure is below 200 liters, and a couple of other small checks. Pre framework days, I'd just write a function called isWaterUsageAcceptable() which would return true or false.

Doing it proper MVC style with CodeIgniter, where would such functions belong?[/quote]

I would put that in a helper, unless only one controller needs to use it then I'd put it in the controller as a function and prefix it with an _underscore.




Theme © iAndrew 2016 - Forum software by © MyBB