I have a CI app that more or less displays tables. On those pages, I’m using PHPExcel to export the results. Right now, the logic in the controller looks like this:
if( $this->input->get('export') == 1 ) {
// Get Data
// Load up library stuff
// Prompt for download
die();
}
$this->load->view('index');
It’s a bit more complicated, you can select CSV/XLS, etc but that’s it in a nutshell. Since this code will be repeated, I’d like to make it a method somewhere else that takes an array of settings, for things like the file name and type. It seems like more than a helper.
Is this my first run-in with a private controller method? If so, what would that look like?
If you’ll need to use this functionality in multiple places, then a Helper is what you’re looking for. However, if the functionality could be broken up into multiple methods, then you might be looking for a Library.
In my thinking, a Helper file is a set of related functions that can be called from anywhere in your application. However, these functions are a little autonomous – meaning that they can be called separately from each other. So a “Date” helper would be a good example. You’d have separate functions to format dates different ways. There really wouldn’t be any need for a constructor, set up, tear down, etc.
A Library on the other hand is more encapsulated than a Helper. You might have the need to run a constructor and call different methods based on any variables you might pass into the constructor.
A Library is object oriented, so it seems to fit what you’re needing.
Private controller methods are really just like helper functions, but don’t need to be called from anywhere – they’re only applicable to the controller methods within the same controller.
I’d create a library and go with that.