Controller
CODE
/**
* Front Controller
**/
class Controller
{
/**
* Getting request, content:
* module => name of module
* action => name of action
* parameters => array of parameters
* param_name => value
**/
private $aRequest;
protected $sDefaultModule = 'index';
protected $sDefaultAction = '__default';
public $hRequestAction;
function __construct()
{
$routing = new Router;
$this->aRequest = $routing->setRequest();
$this->doAction();
}
private function doAction()
{
if(file_exists(MODULES.$this->aRequest['module'].PHP))
{
require_once(MODULES.$this->aRequest['module'].PHP);
if(class_exists($this->aRequest['module']))
{
$this->hRequestAction = new $this->aRequest['module'];
if(method_exists($this->hRequestAction, $this->aRequest['action']))
{
$action = $this->aRequest['action'];
$this->hRequestAction->$action($this->aRequest['parameters']);
}
else
{
$action = $this->sDefaultAction;
$this->hRequestAction->$action();
}
}
else
{
$this->doDefaultAction();
}
}
else
{
$this->doDefaultAction();
}
}
private function doDefaultAction()
{
require_once(MODULES.$this->sDefaultModule.PHP);
return $this->hRequestAction = new $this->sDefaultModule;
}
}
/**
* Class autoloading
**/
function __autoload($name)
{
if(file_exists(SYS.$name.PHP))
{
require_once($name.PHP);
}
}
?>
Router
CODE
class Router
{
private $sRequest; // Przychodzący
private $aRequest; // Tablica
protected $sDelimiter = '/'; // Delimiter np. www.inet.pl/dsd/dsd/dss
function setRequest()
{
$this->sRequest = $_SERVER['REQUEST_URI'];
$this->prepareRequest();
$this->explodeRequest();
$this->getActions();
return $this->aRequest;
}
private function prepareRequest()
{
if(substr($this->sRequest, 0, 1) == $this->sDelimiter)
{
$this->sRequest = substr($this->sRequest, 1);
}
if(substr($this->sRequest, -1) == $this->sDelimiter)
{
$this->sRequest = substr($this->sRequest, 0, -1);
}
}
private function explodeRequest()
{
$this->sRequest = explode($this->sDelimiter, $this->sRequest);
}
private function getActions()
{
$this->aRequest['module'] = array_shift($this->sRequest);
$this->aRequest['action'] = array_shift($this->sRequest);
$this->aRequest['parameters'] = array();
for($count = 0; $count < sizeof($this->sRequest); $count = $count+2)
{
$this->aRequest['parameters'][$this->sRequest[$count]] = isset($this->sRequest[$count+1]) ? $this->sRequest[$count+1] : '';
}
}
}
?>
Jest w tym kodzie kilka rzeczy które nie dają mi spać, może wyszczególnie od punktów zeby można było łatwiej to przeanalizować :
1. Sposób wywoływania akcji, czyli ogólnie cała funkcja doAction() z kontrolera
2. Przekształcanie zapytania czyli Router, czy jest zrobiony odpowiednio
3. Komunikacja pomiędzy obiektami
Bardzo bym prosił o opinie, gdyż w sieci jest naprawde mało wartościowych informacji, a analizując pracę innych, szczególnie na forum, bardzo trudno jest się połapać o co chodzi. Z góry dziękuje za pomoc. Pozdrawiam
