Building a PHP Framework: Part 2 - What is a Web Framework?
Tags: php, phpframeworks, webframeworks
Published: 2018-04-26 13:41:53

---
title: Building a PHP Framework: Part 2 - What is a Web Framework?
published: true
description: Learn what frameworks are, what they do, and how they work. I also talk about commonalities among frameworks, what they provide, and I throw in micro-frameworks for good measure!
tags: php, phpframeworks, webframeworks
cover_image: https://thepracticaldev.s3.amazonaws.com/i/7174s3goded6uroeze24.jpg
---

[Part 1 of this series](https://dev.to/mattsparks/building-a-php-framework-part-1---why-seriously-why-3f7g) 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](https://en.wikipedia.org/wiki/Web_framework) 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](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) (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](https://laravel.com/docs/5.6/blade), for example. Quite a few other PHP frameworks have support for [Twig](https://twig.symfony.com/).

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](https://silex.symfony.com/), [Slim](http://slimframework.com/), and [Lumen](https://lumen.laravel.com/).

## 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](https://jeffknupp.com/blog/2014/03/03/what-is-a-web-framework/)
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](https://en.wikipedia.org/wiki/Comparison_of_web_frameworks)
A pretty thorough comparison of frameworks from many different languages.

Originally posted on [DevelopmentMatt.com](http://developmentmatt.com/building-a-php-framework-part-2-what-is-a-web-framework/)