We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

DRY Controllers for CRUD

Let me first state that I am quite new to Phalcon and MVC; so please be kind. I think I am writing more or less the same code for my CRUD operations for every controller I build. I find it boring, tiring, rather annoying and I suspect it is against best practices. I'm interested in getting suggestions on how to best implement DRY as far as CRUD is concerned with my controllers so I do not have to keep writing the same code over and over. I'd also like to get suggestions on the sharing of views in this scenario. Thank you.


When you say "more or less writing the same code" what exactly do you mean? Are your controllers manipulating the same model hence the duplication? Or is it validation code etc? If it's validation, Phalcon does provide this out the box http://docs.phalcon.io/en/latest/reference/models.html#validating-data-integrity

Also do you use the dev tools? If not you can generate a project scaffold which creates a lot of the CRUD methods for you.http://docs.phalcon.io/en/latest/reference/tools.html

Finally you can also create your own components or a baseController that your other controllers extend and common functionality could be contained in 1 of these.