php")){
require_once("app/config/routes.php");
}
$path = array_keys($_GET);
$config = loader::load("config");
if (!isset($path[0]))
{
$default_controller = $config->default_controller;
if (!empty($default_controller))
$path[0] = $default_controller;
else
$path[0] = "index";
}
$route= $path[0];
$sanitzing_pattern = $config->allowed_url_chars;
$route = preg_replace($sanitzing_pattern, "", $route);
$route = str_replace("^","",$route);
$this->route = $route;
$routeParts = split( "/",$route);
$this->controller=$routeParts[0];
$this->action=isset($routeParts[1])? $routeParts[1]:"base";
array_shift($routeParts);
array_shift($routeParts);
$this->params=$routeParts;
/* match user defined routing pattern */
if (isset($routes)){
foreach ($routes as $_route)
{
$_pattern = "~{$_route[0]}~";
Building Better with MVC
[ 216 ]
$_destination = $_route[1];
if (preg_match($_pattern,$route))
{
$newrouteparts = split("/",$_destination);
$this->controller = $newrouteparts[0];
$this->action = $newrouteparts[1];
}
}
}
}
public function getAction()
{
if (empty($this->action)) $this->action="main";
return $this->action;
}
public function getController()
{
return $this->controller;
}
public function getParams()
{
return $this->params;
}
}
?>
What router actually does is find the controller, action, and parameters from a
request URL.
Pages:
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231