Building a PHP Framework: Part 2 - What is a Web Framework?

mattsparks

Matt Sparks

Posted on April 26, 2018

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?

Let’s go ahead a take a look at the ubiquitous Wikipedia definition of 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.

Alright, fair enough: a web framework helps you create web apps. Let’s consider this the high-level view – the definition is true, but it’s really broad. If we only went by this, coffee (which definitely helps me create web apps) could be defined as a web framework. It does, however, continue:

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.

So, to recap: a web framework helps developers build/deploy web apps, automates common developer tasks, and provides things like database access and session management.

Sounds good.

How Web Frameworks Work

In very general terms all web frameworks, regardless of language, work like this:

  1. The framework receives a request.
  2. The framework parses the request.
  3. The framework performs various tasks.
  4. The framework returns a response.

For a more concrete example, let’s pretend we’ve built a web app that gives users the ability to document when and where they’ve eaten tacos. Johnny, a power user of the app, wants to see all of the tacos he consumed in April of 2017. To do this, he logs in to the app and navigates to https://tacodb.taco/tacos/2017/april

Following the example from above:

  1. The framework receives an HTTP GET request
  2. The framework determines that Johnny wants to see his April 2017 tacos.
  3. The framework does work to retrieve the data.
  4. 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

We understand what frameworks are and how they work. Next, let’s talk about some things most all have in common.

First up, the vast majority of web frameworks employ an architectural pattern known as Model-View-Controller (MVC). It doesn’t matter the language: PHP, JavaScript, Python, C++, they almost always use MVC.

On a side note, did you know there were C++ web frameworks? I didn’t.

Anyway, frameworks usually provide some sort of ORM (Object-relational mapper) to interact with the database.

Most have a templating engine/framework. Laravel has Blade, for example. Quite a few other PHP frameworks have support for Twig.

Other items you’ll generally see included with frameworks are: 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, localization, and the list goes on.

What About Micro-frameworks?

There is a sub-genre, if you will, of frameworks known as “micro-frameworks.” As the name suggest, these frameworks provide less functionality than their “full-stack” counterparts. Examples in the PHP world include Silex, Slim, and Lumen.

Thoughts on Analyze And Next Steps

There are a lot of things to consider when building a framework. I’ve yet to fully 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.

Originally posted on DevelopmentMatt.com

💖 💪 🙅 🚩
mattsparks
Matt Sparks

Posted on April 26, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related