gosukiwi's blog

Creating a PHP MVC framework - Part 4

Hello! And welcome to the fourth part of the PHP MVC tutorial, in this part we’ll dive onto the V in MVC, the View.

If you haven’t already, you should read the other parts before this one

The view is in charge of displaying data to the user, it dictates how the data must be shaped, in the web world, the view is generated HTML code.

There are many ways to generate HTML code, the one we were using so far it was the simplest in the PHP world, just embedded PHP, as I specified earlier, the problem with this approach was giving the view layer a bit too much power, making it easy to do some things that should not be done in the view, and should be performed in the controller instead, on the other hand, the advantage is that embedded PHP has no learning curve, as every developer already knows it.

Creating a PHP MVC framework - Part 3

Welcome to the third part of the PHP MVC tutorial! In the following series we’ll dig deeper onto MVC, in this part we’ll work on the “M” part of MVC, that’s the model!

As BackBone fundamentals describes “The Model represent domain-specific knowledge and data in an application (bussiness data). Think of this as being a type of data you can model — like a User, Photo, or Todo note.”, it is in fact describing the classic MVC, but it applies to web MVC.

It’s important to note that the original implementation of MVC is not the one you see on the web today, it has changed so much in fact it’s actually called Model 2, nevertheless if you ask developers what type of framework Rails (or any other popular Model 2 framework) is, they will most likely answer “an MVC framework”.

It changed because the HTTP protocol is stateles, every request creates a new communication channel between the application and the user, there isn’t a constant connection, as in the desktop world, so the classic MVC had to be modified to work in this context.

Back to web MVC, most of MVC frameworks come with a nice ORM or API we can use to abstract the database from us, which enables us to easily represent the bussiness data in our application, taking us one level higher by using abstraction, remember how C is a low level programming language and dynamic languages such as PHP are higher level? Frameworks provide an even higher level by using several abstractions.

Creating a PHP MVC framework - Part 2

Hello! And welcome back to the second part of the PHP MVC framework tutorial! In this part we’ll get our hands dirty with some real code, if you haven’t already, you should read the previous part of this tutorial.

Before we start

Have in mind we are actually reinventing the wheel! It’s a common story already, you see library X, you really like it, but it doesnt quite work as you want to, you don’t quite like the way it solves X problem, or you think you can do everyone a favour by doing the “ultimate X library”, it will be easier to use, faster, better, stronger, etc, most of the time you just end up worsening the initial problem!

xkcd's view on new standards

xkcd’s sums it up pretty nicely, you can replace standard with library, text editor, or prety much anything.

Don’t get me wrong! The fact that this happens is not that bad, as some of the new standards are actually better, for example, I personally believe that JSON is a win over XML, and so is YAML.

In this case one could argue it’s understandable as we are just doing this as a learning process, nevertheless I don’t discourage you to try and improve the framework or even create your own from scratch!

Don’t get dissapointed to create anything new, new things are cool, but have in mind it’s not as easy as it seems!

Now with that in mind, let’s start our framework :)

Creating a PHP MVC framework - Part 1

Understanding MVC and why frameworks solve problems the way they do is sometimes hard, and even more if you never really had those problems in the first place!

I’m a pretty stubborn person, I like learning by myself, so most of the time I end up looking for those problems, and finding solutions by myself, which enables me to think understand why and how frameworks such as Symfony or CodeIgniter solve those problems.

The whole purpose of these tutorials is face those problems and find solutions, the beautiful part is that by the end of the series you will not only understand the basic of web MVC, but also the most common problems, and have your preferred solutions.