January 19, 2021
Hot Topics:

Build your own MVC Framework - Step One: Getting Started

  • By Marc Plotz
  • Send Email »
  • More Articles »


Welcome to the first part of our voyage into the world of MVC. Our mission: To build an MVC framework that is:

  1. Simple
  2. Extensible
  3. Lightweight
  4. Versatile
  5. Fast-loading
  6. Self Templating
  7. Reusable
Will we succeed? Why, of course we will! Note that all the code in this article is downloadable HERE, although I recommend actually coding this yourself if you are actually trying to learn anything from it.

Perhaps I should explain the motivation behind this system before we get started.


Being a PHP Developer interested in the Model-View- Controller design pattern, I naturally gravitated towards what are supposed to be the greatest MVC frameworks available. However, their enormous footprints, slow loading times and complicated form building systems quickly made me think twice about using them. Why not just write something myself, that does only what it needs to--and leave the heaps of needless code in the trash? A few weeks of careful thinking has lead me to the point where I am ready to tackle this task head on.

Core Components

As a start we need to get a simple MVC system running, and to do that we need the following components:

   • Controller
• Model
• View & Layout
• Router
• Registry
• Access Control
• Sessions

Directory Structure

We start with the following directory structure:

Figure 1: My Project-Framework

The idea is that the core scripts of our website lie in a directory outside of the web root. This means that the main script of the website is essentially not accessible by ordinary means. Our library--the main "brain" of the framework lies outside the framework application script. The reason for this separation will become apparent as we continue.


If you are unfamiliar with the concept of bootstrapping, do not be afraid. It is simpler than it sounds. Consider the Figure 2 below:

Figure 2: Index, Bootstrap and .htaccess added

I'm sure you understand that the public_html folder is our web root. Thus, going to the url http://sitename.domain, our browser is automatically pointed to the index.php file-- assuming no index.html file exists. Bootstrapping is essentially the streaming of data through the index.php file in the webroot to the application residing outside of the webroot, and back again. Having a single entry point into the website--in this case index.php, gives us complete control over the application. What we are going to do is use our .htaccess file to tell the server that no data is allowed to be served from anywhere except index.php. Our .htaccess file lies inside public_html, as illustrated in Figure 2, and has the following code inside it:

Figure 3: Code - .htaccess

What this basically says is that for any request other than index.php, simply go to index.php anyway. Any variable passed after index.php in the url will make up our framework request variable that will be parsed by the router.

Next, we go to index.php itself. We need to include the bootstrap file--which is the whole point of the exercise. Basically, we do not want anything important to be done inside index.php, so we do the important stuff outside of the web root and just include it in the index.php file. For now we will want error reporting and session to work, so we will do that here too, right at the beginning. Later we will put this into a session class perhaps.

Figure 4: Code - index.php

You might have noticed that I did not close the PHP Tag in Figure 3. This is not an error. Closing the PHP Tag leaves room for errant whitespace to be left after it, which can cause all kinds of problems later on down the line. In MVC leaving PHP Tags open at the end of a file is good practice, and don't feel bad about it either--it is not required by PHP anyway.

Page 1 of 2

This article was originally published on July 16, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date