не много измененный вариант с указаного линка
<?php
/**
* паттерн Front Controller
* @link http://phpmaster.com/front-con ... rn-1/
*/
class Route {
protected $controller = 'Controller_Index';
protected $action = 'action_index';
protected $params = array();
protected $basePath = 'index/';
public function __construct(array $options = array())
{
if (empty($options))
{
$this->parse_Uri();
}
else
{
if (isset($options["controller"]))
{
$this->setController($options["controller"]);
}
if (isset($options["action"]))
{
$this->setAction($options["action"]);
}
if (isset($options["params"]))
{
$this->setParams($options["params"]);
}
}
}
protected function parse_Uri()
{
$path = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/");
$path = preg_replace('/[^a-zA-Z0-9]\//', "", $path);
list($controller, $action, $params) = explode("/", $path, 3);
if (!empty($controller))
{
$this->setController($controller);
}
if (!empty($action))
{
$this->setAction($action);
}
if (!empty($params))
{
$this->setParams(explode("/", $params));
}
}
public function setController($controller)
{
$controller = 'Controller_'.ucfirst(strtolower($controller));
if (!class_exists($controller))
{
throw new \InvalidArgumentException(
'The action controller '.$controller.' has not been defined.');
}
$this->controller = $controller;
return $this;
}
public function setAction($action)
{
$action = 'action_'.$action;
if (!method_exists($this->controller, $action))
{
throw new \InvalidArgumentException(
'The controller action '.$action.' has been not defined.');
}
$this->action = $action;
return $this;
}
public function setParams(array $params)
{
$this->params = $params;
return $this;
}
public function run()
{
call_user_func_array(array(new $this->controller, $this->action), $this->params);
}
}