Location: PHPKode > projects > Phpwebpad > phpwebpad/bin/core/Request.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 convert the server url to valid request.
* It reads the url then clean it up and tries to 
* split the url into three different portion.
* e.g. /user/settings/1/hafij?edit=true
* controller name: user.
* action name: settings.
* data: array(0 => 1, 1 => hafij).
* Any $_GET or $_POST will be ignored so that it can
* be used in the controller itself.
*/
class Request {
  private static $URI = null;
  private static $controller_name = DEFAULT_CONTROLLER_NAME;
  private static $action_name = DEFAULT_ACTION_NAME;
  private static $data = array();

  /**
  * This parse method will clean up the ruquest url.
  * It splits the url into three parts (controller, action, data).
  * @Exception when the request is empty.
  * @param string $URI
  */
  public static function parse($URI) {
    if(empty($URI)) throw new Exception('FATAL: Request is empty.');
    self::$URI = $URI;

    if(strstr($URI, "?")) $URI = substr($URI, 0, strpos($URI, "?"));
    $URI = trim($URI, "/");
    $params = explode("/", $URI);

    if($params[0] != "") {
      self::$controller_name = $params[0];
    }

    if(isset($params[1]) && $params[1] != "") {
      self::$action_name = $params[1];
    }

    $data = array();
    $j = 0;
    for($i = 2; $i < count($params) ; $i++) {
      $data[$j++] = $params[$i];
    }
    self::$data = $data;
  }

  /**
  * Let only the router class to call this method.
  * @param string $controller_name.
  * @param string $action_name.
  * @param array $data.
  */
  public static function modifyAsRouter($controller_name, $action_name, $data) {
    self::$controller_name = $controller_name;
    self::$action_name = $action_name;
    self::$data = $data;
  }

  public static function getControllerName() { return self::$controller_name; }
  public static function getActionName() { return self::$action_name; }
  public static function getData() { return self::$data; }
  public static function getURI() { return self::$URI; }
}
?>
Return current item: Phpwebpad