Building a PHP Framework: Part 2 – What is a Web Framework?
Part 1 of this series detailed why I have this crazy idea to build a PHP framework. In this post I’ll be discussing what web frameworks are, what they do, and give some initial ideas for Analyze.
What is a Web Framework?
Here is how Wikipedia defines a web framework:
A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs.
Web frameworks provide a standard way to build and deploy web applications. Web frameworks aim to automate the overhead associated with common activities performed in web development. For example, many web frameworks provide libraries for database access, templating frameworks, and session management, and they often promote code reuse.
To recap: a web framework helps developers build and deploy web apps, automate common tasks, and provides solutions for everyday undertakings.
How Web Frameworks Work
In general terms all web frameworks, regardless of language, work like this:
- The framework receives a request.
- The framework parses the request.
- The framework performs operations.
- The framework returns a response.
Let’s imagine we’ve built a web app for documenting taco consumption. One user, Johnny, wants to see all of the tacos he ate in April of 2017; he logs in to the app and is routed to https://tacodb.taco/tacos/2017/april
Following the example from above:
- The framework receives an HTTP GET request
- The framework determines that Johnny wants to see his April 2017 tacos.
- The framework does work to retrieve the data.
- The framework builds a response and returns it to Johnny.
I’ll be going into a lot more detail about requests and responses soon. In the meantime if you’re not familiar with these concepts be sure to checkout “Further Reading” at the end of the post.
Commonalities Among Web Frameworks
The vast majority of web frameworks employ an architectural pattern known as Model-View-Controller (MVC). A lot provide an Object-relational mapper (ORM) to interact with the database. Many have a templating engine/framework. Laravel has Blade, for example. Quite a few other PHP frameworks have support for Twig.
Other tools provided by frameworks include: caching, security features (CSRF protection, etc.), tools for form validation, session management, middleware, error handling, logging, authentication, event management, queue management, a query builder, database migrations, database seeding, a testing framework, and localization.
What About Micro-frameworks?
There is a type of framework known as a “micro-framework.” These frameworks provide less functionality than their fully featured counterparts. Examples in the PHP include: Silex, Slim, and Lumen.
Thoughts on Analyze And Next Steps
When building a framework there are a lot of considerations. I’ve yet to decide on the direction I want to take for Analyze. A couple things I do know:
- I’m using an architectural pattern other than MVC. I’m not sure what yet, but I’m researching a few options.
- Analyze will undoubtedly start out as a micro-framework. It may end up being just a micro-framework. Time will tell.
In part 3 I will detail the architecture pattern I’ve decided on and map out how I see Analyze working. Some actual code might be written, too! Who knows?
Further Reading
What is a Web Framework by Jeff Knupp
Jeff’s post is a more detailed overview of what a web framework is. Definitely recommend reading it. Plus, it’s from a Pythonic perspective!
Comparison of Web Frameworks
A pretty thorough comparison of frameworks from many different languages.
Update: Building a PHP Framework: Part 3 – Time For Action
Last Updated August 24, 2018.