Welcome Guest, Not a member yet? Register   Sign In
A reflection about Controllers getting fatter in CI4
#1

(It's a sunny morning with a cup of coffee on the veranda so please forgive this philosophical post)
Been working on my brand new shiny CI4 project for a couple of days now and it's shaping up rather nicely. This project is a planned new REST-based backend for an old monolithic web-project created eons ago. The new UI is made in Sencha ExtJS 7.0.0-EA so it's bleeding edge both frontend and backend.

I have been using CI3 for many projects and consider myself rather fluent in how it works. Looking back at the CI4 project so far I notice a big shift in how I code CI4 compared to CI3 and notably that my Controllers are "fattier" than in my CI3 Controllers. With CI3 I adopted the "thin Controller - fat Model" approach where the Controller basically only called functions in the Model. The Models' code got pretty lengthy but kept the notion of working on the data while the Controller mainly checked input and access. In CI4 I now have lengthy database code that should have gone into the Model in CI4.

Don't get me wrong - I sort of like the new Model in CI4 but it is too much pixie-dust sprinkled over them. With all this magic going on I don't feel confident to add code in the Model that may upset the delicate machinery, breaking things. The (imagined?) one-to-one mapping between a Model and a database table may be part of my thinking around this. The mixing of the Query Builder and the Model is another thing that really scares me and I tend to use the Query Bilder completely separate from the Model.

All my hesitation might be just me over-thinking stuff but we need to deliver a fast and reliable solution to our customers and users and I need to feel as confident with CI4 as I did with CI3.

A big thank you for moving CI4 forward!
/Mattias
Reply
#2

I try to keep my controllers small, keeping only form validation, user permissions and that kind of stuff. If I feel I have too much business logic to put all in the same model, I will create some libraries that I will call from the controller. I don't think you should be afraid to break things in the new CI4 model. There's not that much magic. It just do the repetitive and boring DB stuff for you. You can add all the functions you want. I also recommend you dive in the code and look what it's actually doing, it helps to understand what is happening "magically".
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

You can still do everything using just the builder by adding custom methods to your model class. That solves both your concerns - less "pixie dust" in the model methods, and keep your controller thin.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB