Witam,
Próbuje napisać klasę Router do swojego systemu, jednak chyba coś mi nie wychodzi. Proszę spojrzeć.
<?php
class Router {
private $url;
private $params = array();
public function __construct() {
$this->url = $_SERVER['PATH_INFO'];
$this->getParams();
}
// pobiera nazwę serwera
public function getServerName() {
return $_SERVER['HTTP_HOST'];
}
public function getParams() {
$this->params = explode('/', $this->url); }
// pobiera parametr o wyznaczonym id
public function getParam($id) {
return (isset($this->params[$id])) ?
$this->params[$id] : false; }
// pobiera nazwę modułu np. news, page, download
public function getModule() {
return $this->getParam(0);
}
// pobiera nazwę akcji np. edit, add lub delete
public function getAction() {
return $this->getParam(1);
}
// tworzy adres URL np. wpisując createUrl('news/edit/2'); otrzymamy domena.com/index.php/news/edit/2
public function createUrl($params) {
return $this->getServerName().'/index.php/'.$params;
}
}
?>
Czy dobrze zrozumiałem założenie tej klasy?