Location: PHPKode > projects > Phpwebpad > phpwebpad/bin/core/Router.php
<?
/** -------------------------------------------------------------------------------------*
* Version: 1.0                                                                           *
* License: http://phpwebpad.hafij.com @copyright from 2010                               *
* ---------------------------------------------------------------------------------------*
* DEVELOPED BY                                                                           *
* Mohammad Hafijur Rahman (Badal)                                                        *
* hide@address.com, hide@address.com                                             *
* ------------------------------------------------------------------------------------ **/

/**
* This class route the request to a valid controller. Any request must look
* for a controller file. Sometime we may have some special route. But first
* router will check the available valid controller file to render. If it can't 
* find any valid controller then it will look into the special controller.
* e.g. /user/settings; in this request user is the controller and settings is 
* the action. So router will render the usercontroller file and then the action
* settings method will be called in that usercontroller class. This is so general.
* But there are some other kind of valid request may take place. 
* e.g. /user  - valid because index is the default action if not found. The 
* special rout can consist of data. e.g. /hafij or www.something.com/hafij.
* Because sometime we want to show nice and user friendly link to the user.
* In this case hafij is just a username of the user. So if we want the url
* to work we need to have special route for this to render the controller.
*/
class Router {
  private static $special_route = array();
  

  /**
  * Start the router. First we try to find the controller file
  * if not then we check the special route.
  * @Exception if the request can not be served.
  * @return void.
  */
  public static function start() {
    if(!file_exists(self::getControllerFile()))
    if(!self::isSpecialRouteThenModifyRequest())
      throw new Exception("FATAL: Request can not be served.");

    self::render();
  }

  /**
  * Render the controller, action, and view file.
  * @return void.
  */
  private function render() {
    include_once(self::getRootControllerFile());
    include_once(self::getControllerFile());
    $dyn_class_obj = self::getController();
    $dyn_class_obj->beforRender(Request::getActionName());
    $dyn_class_obj->renderAction(Request::getActionName(), Request::getData());
    $dyn_class_obj->afterRender(Request::getActionName());
    $dyn_class_obj->renderView(Request::getControllerName());
  }

  /**
  * Controller class object that extends root controller.
  * e.g. ApplicationController extends Controller.
  * This methode will return ApplicationController object in this example.
  * @return Contoller object.
  */
  private static function getController() {
    $class_name = ucfirst(Request::getControllerName()) . ucfirst(ROOT_CONTROLLER_NAME);
    $dyn_class_obj = new $class_name();
    return $dyn_class_obj;
  }

  /**
  * Return the file path of the controller.
  * Here we dont check if the file exist or not.
  * @return string.
  */
  private function getControllerFile(){
    return CONTROLLER.ucfirst(Request::getControllerName()) .
    ucfirst(ROOT_CONTROLLER_NAME).".php";
  }

  /**
  * Return the file path of the root controller.
  * @exception if thr file does not exist.
  * @return string.
  */
  private function getRootControllerFile(){
    $path = CORE.ucfirst(ROOT_CONTROLLER_NAME).".php";
    if(!file_exists($path)) {
      throw new Exception('Root controller file can not be found.');
    }
    return $path;
  }

  /**
  * Check if the request is the special route.
  */
  private static function isSpecialRouteThenModifyRequest() {
    $flag = false;
    
    foreach(self::$special_route as $r) {
      if(preg_match($r[0], Request::getURI())) {
        Request::modifyAsRouter($r[1], $r[2], 
          array_merge(array(Request::getControllerName(), Request::getActionName()),
            Request::getData()
          )
        );
        $flag = true;
        break;
      }
    }
    return $flag;
  }

  /**
  * Add special request to the router.
  * @param string $reqularExpression
  * @param string $controllerName
  * @param string $actionName
  */
  public static function addRoute($reqularExpression, $controllerName, $actionName) {
    if(empty($controllerName) || empty($actionName))
      throw new Exception('Can  not add special route. Controller name and
                           action name can not be empty');
    
    self::$special_route[] = array($reqularExpression, $controllerName, $actionName);
  }
}
?>
Return current item: Phpwebpad