We are developing a module and the controller mcp file has become quite large. I wondered if there is a best practice for splitting this into multiple files. It's a collection of methods in the class our module_mcp class, so can these be split out into other files somehow? Multiple controllers instead of one? We could use the include() method in PHP but doesn't seem it would be the best practice to do this.
-
2Are you already using a separate data model class for your data fetching/saving? That could cut out a lot of lines from your .mcp file if not.– Derek HogueNov 20, 2013 at 18:18
-
Yes we have models for the database layer. but the controller file is quite large now, I am used to having separate controllers in codeigniter, but EE seems to have just one controller for a whole module.– Laurence CopeNov 21, 2013 at 9:15
1 Answer
I would suggest a couple things to help separate our your code.
- As Derek mentioned in a comment above, make sure you're abstracting your data model work to a model class.
- You can also create a libraries directory within your add-on package and begin to create your own set of libraries. Since they are local to your package, you'll be able to easily load them like
$this->EE->load->library('your_module_class_name');
Then, within the control panel file you can simply pass off processing to your library. As the code gets more complex, you can abstract into multiple libraries as needed.
Of course all of this is really a matter of your own personal taste and need for organization. You want to be careful not to abstract too much, making it incredibly difficult to trace through later.
-
I am using libraries for a few things, but maybe I have not quite grasped the full use of libraries, I shall look into it a bit more. As stated in my comment above, I am used to working with multiple controllers in codeigniter, but EE modules have one controller file... so I wanted to see how to revert to a traditional multiple controller setup or similar. I hate having my entire application controller code in one file! Nov 21, 2013 at 9:17
-
-
So I have moved my controller code into a library, load the library, initiate the class, and then call the library class method from within the controller method and it all works a treat :). Ah feels nice to be organised and tiny again. Thanks. I had some common functions in my mcp file, but I just moved those to a helper. Nov 21, 2013 at 21:02
-