CodeIgniter MVC Workflow – Best Practices and Tips

codeignitermvcPHPworkflows

Bit of a workflow question.

I'm just staring with MVC and wondered how other people usually work in MVC? The 2 options I see is to code the model and controller completely first and then work on the views after. The other option is to code all parts at the same time.

I'm looking for what might be the quickest most efficient way to code in MVC? We don't have inhouse designers or designated jobs so everyone does the same tasks (coding, designing, testing, db, etc).

Thanks!

Best Answer

There's a third option, start by writing your tests. It's the better option, and it's not specific to MVC. If you do start by writing tests, then the next step depends on your approach to MVC:

  • Fat controllers / skinny models

    Your controllers do all the heavy lifting, while your models are essentially POPOs, tasked with maintaining state between requests.

  • Skinny controllers / fat models

    Your controllers are limited to passing stuff around, most (if not all) of your business logic lives in your models.

CodeIgniter favours the first approach but it doesn't restrict you from the latter one. It's completely up to you which one you'll follow, personally I'd strongly suggest the second approach, simply because with fat models you'll get the chance to test your business logic very early in the development cycle.

With skinny models, you'll have to build both your controllers and your models before you'll be able to test your business logic, and that's not particularly efficient. Your business logic is the more important part of your application, I think it's obvious that the sooner you build and test it, the better.

To summarize, my workflow is:

  1. Write the business logic tests,
  2. Write the models that would satisfy those tests,
  3. Pretend I practice TDD for everything else,
  4. Write the controllers and views.

Further reading: