Basics of Framework Design in PHP

If you consider developing a framework you should begin with the logic of framework design. This article will show you the concepts of framework building. We will be discussing framework logic with MVC pattern in mind.

First we have to understand what the framework should do. Frameworks help developers minimizing repetitive code that is easily manageable and reusable. If we break up a PHP web application (generally websites) we find that some areas of the code could be isolated from the rest of the logic and reused in other projects as well. We like to call these codes modules.

Now we have to understand the code flow. What happens when a user visits our website, makes a request. For example if a user requests to see the ‘work’ section on our website (www.primalskill.com/en/work) the application figures out what section of the website is required, gets the data from a database, formatting it so it will be nice and sends it to the user.

By figuring out which section is requested is generally done looking at the URL. In the example above you can see in the URL there is the word ‘work’. This means the user wants to see the websites ‘work’ page. In framework thinking this could be a module. But that’s a little bit more complicated.

In a module based framework a request looks something like this:

Framework Flowchart

The front controllers job is to load the general classes, the master configuration files and based on the request figure out which module to load.

The module job is to get the requested data from a database, file, etc. format the data and send it back to the user.

In the front controller layer we can implement security and URL routing filter classes. We can load a database abstraction class and pass it to the module.

It’s a good practice to keep your files strictly organized in folders with access rights and so on. I will present here a general project folder structure:

Framework Structure

Here you can see the general structure of a project. The ‘root - libraries’ folder holds all the common classes that a modules may use, like database abstraction class, error handler class, etc. The ‘master’ folder contains a main front controller. This file loads general modules, master configuration and resolves which module to load. Each module has separate controllers, models, views and configuration specific to that module.

It’s a best practice to turn the error reporting off when you put the application online, but also know when an error is occured, that’s why is a good advice to make a logs folder and put the exception handler generated files in here.

Of course there are lot’s and lot’s of other file structuring frameworks, this is just one of that many. I hope you enjoyed reading this article and check back soon for new ones.

  1. Avoid Clutter
  2. MIR:ROR. RFID and it’s Biggest Problem

Comments
  1. Sean Nieuwoudt
    August 23rd, 2008

    Good Read! thanx! i was putting something similar together.

    Sean

  2. [...] I saw that the first part of this post (Basics of framework design) got a little bit attention I decided to continue that thought. Before you continue reading make [...]

Leave your reply
  • (required)
  • (will not be published)
  • Show
    :mrgreen: :neutral: :twisted: :arrow: :shock: :smile: :???: :cool: :evil: :grin: :idea: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: 8-) 8-O :-( :-) :-? :-D :-P :-o :-x :-| ;-) 8) 8O :( :) :? :D :P :o :x :| ;) :!: :?:
  • Allowed tags: <a href="" title="">, <b>, <i>, <strike>, <em>, <strong>, <code>