Thursday, June 2, 2011

FrontController for PHP



PHP is one powerful tool for building web application. Like in my previous work, PHP is their main choice of language when making a web site. Of course, the application is with the vital combination of javascript and CSS to make it presentable.

Constant research allowed me to see that there were a lot of PHP frameworks presented on the web. There were even comparison of each other and whose the best maybe. Take Zend for example. It offers you a lot. However, you must understand that there are functionalities that are unfamiliar to you which careful measures must be considered before implementation.

But I'm not against those frameworks because in reality, they were already tested and proven effective in rapid application development. In fact, they ease the work load double fold. I'm not in doubt of their capacity(frameworks) but of course I would like to learn how did they do it.

I understand that complex codes are very difficult to follow and understand so I'd like to throw a simplier solution to those developing website in PHP using the FrontController.

What is a FronController? It is simply the gateway to an application. All request must hit the front controller leaving it to decide what to do next. A web application typically has to set up sessions, connect to database, and delegate variety of request to controllers. And that's what FrontController has to do. What happens here is the program flow logic.

A simple representation of this is the code below:

index.php
<php
  session_start();
  $redirect = !empty($GET["page"] ? $_GET["page"] : "home";
  switch($redirect){
   case "home":
      include "pages/home.php";
      break;
   case "account":
      include "pages/account.php";
      break;     
  }
?>


A sort of procedural programming works, but the benefit of doing it in a object oriented fashion produces a more scalable and flexible controller architecture. Should the base be there, its up to you to make it more magical.

OOP Approach
index.php
<php
  define("APP_DIR", dirname(_FILE_) . "/pages");
  require_once "FrontController.php";
  FrontController::instance()->redirect();
?>
FrontController.php
<php
class FrontController{
 public static function instance(){
  if(!defined("APP_DIR")){
    ext("Severe: Cannot proceed without access to application 
        directory.");
  }
 }
 
 public function redirect(){
  $page = !empty($_GET["page"]) ? $_GET["page"] : "home";
  $action = !empty($_GET["action"] ? $_GET["action"] : "default";
  
  $object = ucfirst($page) . "Controller";
  $file = APP_DIR . "/" . $page . "/" . $object . ".php";
  if(!is_file($file)){
   exit("Severe: Controller not found!");
  } 
  
  require_once $file;
  $method = "do" . ucfirst($action);
  $controller = new $object();
  if(!method_exists($controller, $method)){
    exit("Severe: Action not found.");
  }

  $controller->$method();
  exit(0);
 }
}
?>
account/AccountController.php
<php
class AccountController{
 public function doPrintFName(){
   echo "Your are in AccountController with doPrintFName function";
 }
 public function doPrintLName(){
   echo "Your are in AccountController with doPrintLName function";
 }
}
?>
Now, invoke url adding this parameter: index.php?page=account&action=printFName. You should be able to see the message, Your are in AccountController with doPrintFName function. Invoke index.php?page=account&action=printLName, what can you see?

In here, I showed a very simple pattern how you can achieved a healthier PHP application with a pattern which helps you debug faster. There is a better way to achieve MVC pattern in PHP. I'll do it later...

Learn More:

No comments:

Post a Comment

There was an error in this gadget