//The simple script can route request to your website to a predefined application page
as you wish//
//Routing or dispatching is done using url splitting and mod rewrite in apache server//
class Dispatcher
{
var $uri;
var $uri_components;
var $uri_path;
var $uri_query;
var $uri_self;
var $server_address;
var $server_name;
var $controller;
var $action;
var $config_success = false;
var $action_object;
var $action_file;
function getUriComponent()
{
$this->uri = $_SERVER['REQUEST_URI']; //server request url //
$this->uri_components = parse_url($this->uri);
$this->uri_self = $_SERVER['PHP_SELF'];
$this->uri_path = $this->uri_components['path']; //path to request //
$this->uri_query = $this->uri_components['query']; //query string for the url//
}
function getUriPath()
{
return $this->uri_path; //path to request //
}
function getUriQuery()
{
return $this->uri_query;
}
function getController()
{
$self_explode = explode("/",$this->uri_self);
$path_explode = explode("/",$this->uri_path);
$self_position = array_keys($self_explode,"index.php");
$path_start_up = $self_position[0]; //
$controller_load = $path_explode[$path_start_up];
$controller_action = $path_explode[$path_start_up+1];
if(empty($controller_load))
{
$this->controller = "index";
}
else
{
$this->controller = $controller_load;
}
if(empty($controller_action))
{
$this->action = "index";
}
else
{
$this->action = $controller_action;
}
}
function callException($error_string)
{
trigger_error($error_string,E_USER_ERROR);
die("<br>Exception point Reached ".__FILE__);
}
function getControllerObject()
{
$this->controller = strtolower($this->controller);
$this->controller = ucfirst($this->controller);
return $this->controller;
}
function initRouter()
{
$this->getUriComponent();
$this->getController();
$this->controller;
$this->action;
$LoaderObject = new Loader(); //creates a new Loader Object //
if($LoaderObject->isControllerExists($this->controller))
{
if(($this->action_object=$LoaderObject->isActionExists($this->controller,$this->action))!=false)
{
//echo "Config Loaded";
$this->config_success = true;
}
else
{
$this->callException("AxF Error Point : Action Not Found");
}
}
else
{
$this->callException("AxF Error Point : Controller Not defined");
}
}
function isLayout($layout)
{
if(file_exists("application/Layout/".$layout))
{
return true;
}
else
{
return false;
}
}
function getActionFile()
{
return $this->action_object;
}
function setLayout($layout)
{
if(file_exists("application/Layout/".$layout))
{
$file_inc = "application/Controller/".$this->getControllerObject()."/";
$file_inc = $file_inc."".$this->getActionFile();
$this->action_file = $file_inc; //module file//
//include("application/Layout/".$layout);
}
else
{
$this->callException("Layout file not exists ");
}
}
function include_action()
{
include($this->action_file);
}
function include_layout_file($file)
{
include("application/Layout/".$file);
}
}
Download complete source code
|