On a walk I was wondering do we need all the abstractions frameworks and programming theories make us use? So I was thinking what if I only had a webserver and PHP how would I do it.
Router and middleware
By adding files and directories to the publicly exposed directory of the webserver it is possible to create routes.
For dynamic sections in routes, set a catch all file in the webserver config. This is the way all frameworks start their request processing.
Webservers have the capacity to limit the HTTP methods for an URL path.
Webservers can protect directories with user/password authentication, OAuth/OpenId authentication, IP whitelisting, rate limiting. With frameworks the equivalent is using middleware.
Controller
There is a mantra to make the controller thin, and I think this is perfect for the PHP files in the public webserver directory.
This is the file where the application takes over the middleware functionality the webserver is not capable of, like authorization.
Instead of having a container, instantiate the dependencies needed for the business layer and let it take over until it is time to return a response.
A basic response is nothing more than a text with headers and a body.
A modern feature like early hints is just an extra header.
This could be a function instead of an object.
Model
With DTOs and the advanced capacity of databases it feels like a model is a useless abstraction.
Functions or repositories are enough to give the database storage manipulations enough context.
View
Just use PHP files for the templates. It is not as forgiving as the templating engines, but that can be a good thing.
Conclusion
In a time were we are so used to build abstractions upon abstractions, it feels good to remove them instead of adding them.
While this is not the "right way" in every situation, use it as a guiding star the next time you start a project.
Top comments (0)