1

I'm using CodeIgniter and I have a few utils that I want each controller (that is, each controller file) to have access to.

The question is: where to put these?

I thought of helpers, but the CI documentation talks only about extending existing helpers, not making your own. I'm sure that doesn't mean I can't make my own helpers, but it does mean I don't know how they should be built (procedural? Methods of the global CI instance? etc)

I also considered hooks, but this is a poor fit I think as I'm not extending core functionality.

Or is there some other way I'm missing?

tereško
  • 56,151
  • 24
  • 92
  • 147
Mitya
  • 30,438
  • 7
  • 46
  • 79
  • 1
    CodeIgniter 2.1.4? Helpers are generally just files with stand-alone functions which can be called upon for very distinct tasks. If you want your entire application to have access to it then add it to the `/application/helpers/specific_tasks_helper.php` folder and then autoload it in `/application/config/autoload.php` by adding `specific_tasks` to the `$autoload['helper']` array – MonkeyZeus Jun 17 '14 at 20:19
  • Thanks for this @MonkeyZeus – Mitya Jun 17 '14 at 21:29

2 Answers2

2

It's been a while since I've done this but I believe I used two approaches.

Community
  • 1
  • 1
dispake
  • 3,183
  • 2
  • 16
  • 21
1

I did it simply by adding a file to the application/helpers folder (maybe I created that folder - I can't remember) and then loading them in the usual way.

Kryten
  • 13,116
  • 3
  • 41
  • 61
  • The thing is, though, helpers seem to be for procedural, static functions, whereas I'm looking to use common methods, i.e. methods all of my controllers have access to. – Mitya Jun 18 '14 at 11:45
  • You mentioned helpers in your question - in CI, helpers are functions. If you want class methods that all controllers have access to, then you're talking about something different. I think what you're probably looking for is a common base class for all your controllers to inherit from - if that's the case, then you can put your base class in the controllers directory. – Kryten Jun 18 '14 at 16:12
  • Yeah this is the point another commenter was making. I was unaware of the existence of these before today :) Thanks. – Mitya Jun 18 '14 at 17:35