Maintainable code and controllers


In my App, the container.config.php contains:

  • Per env specific config (db, webservices, full path to filesystem to save some files, logger config, controllers and middlewares init.

My Controllers are as follow:

  • PublicController: all public pages: homePage and password reset request/processing mostly,
  • CustomerController: this is the biggest containing all information accessible to logged users protected by Auth middleware: all the processing, and rendering views, and input values checks are done here in seperate methods.

While this is functional, and that db calls or processing is handled in models and service (CustomerService in this case), It’s kinda bloated.

I was instead thinking of:

  • Creating 2 base controllers:
  1. Public Controller
  2. Customer Controller

Each of those would have separate subControllers per view. Then: 1 view = 1 controller with n actions. For instance for a page showing products:

  • ProductController:
  • showArticles()
  • showArticle()
  • addToBasket()

Is creating controllers this way and adding them to the container.config a good way of doing things?

One advantage it has, is that it’s easy to check for missing or incorrect App settings in one place, the IDE will warn upon instanciation errors and the biggest flaw is that adding controllers involves re modifying the config file in multiple environments.

While this way seem a trivial question, organisation is key to maintainable code.

Care to provide some feedback or large apps?


I use a different approach and it saves me massive amounts of code on larger projects.

My controller for visible actions (ie: things which generate HTML) only has 3 functions:

- detail
- edit
- view

each of these functions takes an extra parameter which tell the function which object is requested. This allows you to have generic controller code (but requires you to offload specific requirements into the model (or some other supporting class)).

It works well for me but I must admit that I’ve been writing code like this for years, so it’s second nature for me.