Skip to content

Posts from the ‘PHP’ Category

26
Mar

Building a simple MVC framework

Introduction

I don’t know about you, but it took me a while to fully grasp the concepts of MVCs. I mean sure, it’s a Model View Controller framework and the model manages the raw data and links it to the database, the controller manages the requests to a specific module and is the translator between “Model-talk” and “View-talk” – which should be mute btw – and the View displays what the controller says to display.

I’ll let you check wikipedia for all the theoretical details and the thousand related frameworks.

Courtesy of the Symfony project

Meanwhile, we have tons of schema explaining to you how they work, like this one, from the Symfony framework. We’ve all seen those, but they don’t really demystify the “How do they work?“. When you look at the code, it’s MASSIVE. And it seems working like magic.

This is a humble attempt to explain the basis of how all those frameworks work for the curious programmer. I’ve been strongly inspired to create this by the Zend framework, Symfony, Yii and a great tutorial found here.

I strongly discourage you from writing your own MVC for production purposes. Use one of the above, take time to learn it and run with it. I personally have a preference for Symfony because it uses Doctrine, which is one of my favorite PHP software ever. It’s an amazing ORM.

That being said…I strongly encourage you to write your own MVC to satisfy your curiosity :)

I don’t really know where I’m going to go with this but if it proves popular, I probably will expand the whole thing, bits by bits, just for fun! If i do though, be prepared for a lot of refactoring though, since there will be many problems. I kept this as simple and straight forward as possible to keep the code distraction free.

This part is all about the dispatcher. How do we fetch the right controller, output the right data. Oh, and one more thing before we get started: we know nowadays SEO is the bread and butter of the web. Therefore, we will directly code for plain text URLs. We will go down the rabbit hole as a request hitting the web server, and see, step by step, what’s going on and how is it working.

Let’s begin!

Read moreRead more