Welcome Guest, Not a member yet? Register   Sign In
Avoiding massive controller: where to put individual files
#5

I have to support legacy code (not written by me) with some controller files with 4000-8000 lines. It is very hard.

My personal choice is skinny controllers. Bit CodeIgniter alone does not help much in this regard.

When a controller is loaded its constructor is executed and then the specific method that router calls is executed. The other methods are "dead code" for the particular request.

For dealing with this problem there is an extreme, experimental maybe approach demonstrated by this framework: https://github.com/bluzphp
Here is a sample controller: https://github.com/bluzphp/skeleton/blob...ssword.php - it is not even a class, it is a function that is loaded only for a particular request. There is no dead code.

Approximate simulation of this could be done in CodeIgniter if you have a separate controller for each possible request with a constructor and index method only. But this is quite extreme. Other methods could be implemented too, it is desireable the controller file not to excede about... let us say 400 lines.

But to split a massive controller file into smaller ones brings another problem - where to put the common code for initialization that usually is put within the constructor. Some people make libraries and then load them manually within all the controllers. This may be done, but it is not convenient. Another way that is better IMO is using base controllers - see this: http://avenir.ro/codeigniter-tutorials/n...ontroller/

If you want to implement some pure classes that are not CodeIgniter-specific, see this: http://forum.codeigniter.com/thread-420.html

So, to have skinny controllers, move code as much as you can within models, libraries, helpers, classes, etc and adopt the base controllers concept.
Reply


Messages In This Thread
RE: Avoiding massive controller: where to put individual files - by ivantcholakov - 12-27-2014, 08:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB