Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter - functions available in multiple controllers

I'm relatively new to CodeIgniter, and so far my project is created entirely using Controllers and Views. However, as it's getting more complex, I'm finding that there are certain functions that I've copy-pasted into multiple controllers -- this is hardly ideal as editing one requires remembering to edit all of the others as well.

There is a plethora of CI features that I know nothing about - models, helpers, extending "Controller", etc. etc. Where should I look to in order to accomplish the above? I suppose I could also import() a block of code directly, although I get the feeling that this is not "the CodeIgniter Way".

like image 585
Mala Avatar asked Feb 24 '23 15:02

Mala


2 Answers

Put all your "utility" functions into a "helper manager" and access that.

http://codeigniter.com/user_guide/general/helpers.html

like image 73
Dominic Tancredi Avatar answered Apr 01 '23 22:04

Dominic Tancredi


Or create a base controller, and extend other controllers from it.

I'm sure Phil Sturgeon has a guide on it: http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

like image 33
bl00dshooter Avatar answered Apr 02 '23 00:04

bl00dshooter