Bobcares

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL

*The maximum is $4000 in credits, Offer valid till December 6th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL

*The maximum is $4000 in credits, Offer valid till December 6th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

Simple, Easy-To-Use PHP Framework :: An Understanding

by | May 26, 2009

PHP has a lot many frameworks and I have gone through many of them. One of the biggest difficulty that I found in some of them was the learning curve. Most of these frameworks use complex directory structures and difficult to remember naming conventions. Also, each one of them uses their own method for adding templates and db access. You don’t get the option to put in something that you are familiar with, like Smarty.

According to me, PHP is a powerful language for web programming, both with regard to speed as well as with the ease of use for web. And when you consider Java for web programming, servlets are a powerful concept. It indeed becomes handy when the design concepts of servlets and the speed and usability of PHP can be combined. This framework is an attempt on that path.

Folder Structure

AppPath:
    /framework : Framework folder
    /classes : For user classes
    config.yaml
    .htaccess

The config.yaml serves as the configuration file for the framework and the application. All the requests to the application will be routed through the framework controller (controller.php). The default framework extension is .do. So, any request with .do extension is considered as a framework request. A sample request is of the form, http://yourdomain.com/login.do.

Having a fairly good understanding of PHP is definitely an advantage when it comes to fully exploiting the features of a PHP framework.

I have added a sample login page for the framework. To see how this works, untar the framework in the web root of your domain, go to http://yourdomain.com/index.php and then check out where the values are posted to.

Installation

  1. Copy the sample project to your web folder.
  2. Edit the configuration file, config.yaml
    • Specify your application root path, application name and framework folder under globals.
      I have also added sections for databases and other configuration details. You can specify the configuration as you require and these will become available for your application. For yaml specifications, check the folder /framework/packages/spyc/.
    • Next comes an important part. There are two sections in this namely, ‘scripts:’ and ‘urlpatterns:’ Here you need to specify your MVC patterns. Under the url patterns, you specify the pattern of URL for the request.

For example, in this section there is an entry for script-name:login and url:/login. The script name is a logical name for the pattern and it can be anything. For a particular script-name in url patterns, there should be a corresponding entry in scripts. You should also specify the class path and class names here without a .php extension.

Now, let me explain the steps to add a new page. This will help to give you a better idea. Let’s say, we need to add a new class for login check. And we are expecting the URL as login.do. In this case, the entries in config.yaml should be as follows:

scripts:
  - script-name: login
    class-path: classes/
    class : Login

urlpatterns:
  - script-name: login
    url: /login

The login class should be APP/classes/Login.php and a request of the form, http://yourdomain.com/login.do should be routed to this class. The next important thing is that the Login.php class should have either one or both the functions doGet($request) and doPost($request). These functions will be automatically called, according to the request method. The doGet function is where your request is handled if it’s a GET. In the case of a POST request, it will be handled in the doPost function. You can get the request parameters by calling it as $request->getParameter($pname).

How does it work

All the requests with framework extension .do, will be routed through the framework controller framework/controller.php

Note: You can change the default framework extension .do, to whatever you wish. It can also be an abbreviation of your application name. For this, you just need to edit the .htaccess file.

PHP is a powerful language for web programming, both with regard to speed as well as with the ease of use for web.

The controller initializes the framework. The first thing it does is loading the config.yaml file. Then the initializeListener will be called, where the main actions are performed. First it calls the HttpRequestWrapper::getRequest, where it wraps the request from the client in a HttpRequestWrapper class. The GET and POST parameters will be wrapped to our class here.

Then it searches for the class that corresponds to the URL pattern, from the loaded config.yaml file. If it finds the class from the scripts in config.yaml, it initializes the class and calls the doGet or doPost methods corresponding to the requests that are received.

Conclusion

This is a simple framework that performs only the basic functions. And yet it’s powerful. You can change the class that should load on request, by simply editing a configuration file. Only requests with .do extension are handled by the framework. You can also have your legacy PHP code along with it. There are a lot more extensions possible for this, like adding template and database frameworks.

Having a fairly good understanding of PHP is definitely an advantage when it comes to fully exploiting the features of a PHP framework. Though it may look overwhelming at first, the time spent in understanding a framework is definitely made up for later, when you start saving time for your development projects.


About the Author :

Biju T, works as a Senior Software Engineer in Bobcares. He joined Bobcares in February 2007, and has experience in developing web based Java and PHP applications. Biju is a SCJP certified Java developer and is passionate about software architectures and design patterns. He loves cricket and badminton, and during his past time, he spends time watching new movies in town.


2 Comments

  1. kind

    Nice!

  2. Håkon Leinan

    Nice. Do you have a license added to this great script? Would like to use it but then it should have some license.

    Great work!

Never again lose customers to poor
server speed! Let us help you.