Currently I'm doing something like this:
Controllers: Immediately call models and then views. No real other logic than deciding which model and view to call
Models: Contain all functions per table in my db. I have one model class with its methods per table in my db.
View: Only contain layout, almost zero logic.
Libraries: All my classes that aren't specific to a database table
Third Party: Downloaded plugins
My question is, is this correct? For instance, should I always put my non-table-specific classes in libraries? What about in cases where I have a class or function that is a combination of, say, "product" and "customer" tables?
Thanks in advance
Your application should not be mixed in with your framework. That means that it should be in its own folder (you can name it after your app). In there I recommend you keep everything pure PHP and if you want to you libraries then use composer to install them.
This allows you to test your application without loading the framework too which is a great benefit. Also if you ever want to switch frameworks, or move to a completely different platform (standalone for example) then it is made a lot easier.
In the application folder you have a class for each user story/use case (Usually verbs, think processOrder). You can put those in a folder called interactors.
Those classes operate on your entities that you put in your entities folder (also in the app folder).
To interact with the application you want to have clear boundaries defined (interfaces). You call your use cases from the controllers and display the result. Your controller is responsible to move the data from the application to the user and the other way around (with the use of views of course).
Your database stuff (including API and other data sources) should be separate too. Again we need clear boundaries. You need to populate your entities with data or provide data directly to your interactors in other cases. I recommend you use repositories for that.
This also allows you to decouple your app from the database part which is great for testing, you can just create a stub repository and then run your tests without having to use the database.
There is a great talk about this from uncle Bob: http://www.youtube.com/watch?v=WpkDN78P884
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With