In most of my web applications I have the following 4 directories:
- Controllers
- Services
- Repositories
- Models
Over the years I’ve been trying to develop as “clean” as possible. I’ve read numerous books and articles. I seemed to have developed a way to keep things neat and tidy, but sometimes it feels like overkill.
It goes something like this:
- Everything starts at the controller. A request comes in. For example, a request to follow a user. I will have a
FollowController. TheFollowControllerhas aFollowServicedependency. - The
FollowControllerwill do any validation checking on the inputs - I then have a
FollowServicewhich has follow/unfollow methods. This service has aFollowRepository, andUserServicedependency. In the FollowService follow method usernames are converted to ID’s, checks if the user exists, and then makes a call toFollowRepository::follow -
FollowRepository::followwill follow the user and make calls to theFollowModel.
In my mind I have the following rules:
- A controller doesn’t make calls to a repository, or a model directly. Only to services
- Services do not make calls directly to models, only repositories or other services
- Repositories only call require models. If possible I try to ensure that each repo is attached to a single model.
At times there’s exceptions to this, in cases when making things overly complex isn’t required.
I just wanted to know if what I’m doing is correct, or whether I’m setting myself up for further issues. I don’t know if what I’m doing has a specific name, or if it’s a known methodology. It’s something I’ve picked up after a long time using Slim.
