Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: Where should a particular functionality go?

Here is a quick overview of the controllers functionality in most of the application:

  • controller loads a specific model, gets data from it, formats the data and passes the formatted data to the view.

Now there is a search page, which needs to do a search query over entire database (all models). It needs to show each type of data in its particular formatted output on a single page as a list.

The problem:

The search controller can do the search, dynamically load model for each record type, and get the data from model. Problem comes when the data needs to be formatted. I am trying to load the specific controller from the search controller, which is causing problems.

What to do?

PS: I tried using the 'Wick' library, but it fails when the controller's format function tries to use its own model and session object, giving errors about call to a member on a non-object.

like image 945
Samnan Avatar asked Nov 05 '22 14:11

Samnan


1 Answers

After much refactoring and trial/error, It appears that the best way to achieve the above is this way:

  1. Keep the format function in the base controller from which all other controllers are derived. The format options are passed to the function along with the data object as arguments.

  2. Make a static function in each derived controller, which returns the formatting options of the data.

  3. Inside the search controller (which is itself derived from the base controller), for each data object, call the static function of its particular controller which returns the data formatting options, then use that to format the object for the view.

I guess I can say I will stick to using the model only for interaction with the database, and let everything else be done by controller. If anyone has a better solution still, I am all ears.

like image 54
Samnan Avatar answered Nov 12 '22 10:11

Samnan