Route
<?php
namespace Avello\
System\Engine\Router
;
/**
* Klasa zawiera pojedyńczy element do routingu.
* @package Avello\System\Engine\Router
* @author Łukasz Socha <kontakt@lukasz-socha.pl>
* @version 1.0
*/
class Route
{
/**
* @var string Ścieżka URL
*/
protected $path;
/**
* @var string Ścieżka do kontrolera
*/
protected $controller;
/**
* @var string Nazwa klasy
*/
protected $class;
/**
* @var string Nazwa metody
*/
protected $method;
/**
* @var array Zawiera wartości domyślne dla parametrów
*/
protected $defaults;
/**
* @var array Zawiera reguły przetważania dla parametrów
*/
protected $params;
/**
* @param string $path Ścieżka URL
* @param array $config Tablica ze ścieżką do kontrolera oraz nazwą metody
* @param array $params Tablica reguł przetważania dla parametrów
* @param array $defaults Tablica wartości domyślne parametrów
*/
public function __construct
($path, $config, $params=array(), $defaults=array()) { $this->path=HTTP_SERVER.$path;
$this->controller=$config['controller'];
$this->method=$config['method'];
$this->class=$config['class'];
$this->setParams($params);
$this->setDefaults($defaults);
}
/**
* @param string $controller
*/
public function setController($controller)
{
$this->controller = $controller;
}
/**
* @return string
*/
public function getController() {
return $this->controller;
}
/**
* @param string $class
*/
public function setClass($class) {
$this->class = $class;
}
/**
* @return string
*/
public function getClass() {
return $this->class;
}
/**
* @param array $defaults
*/
public function setDefaults($defaults) {
$this->defaults=$defaults;
}
/**
* @return array
*/
public function getDefaults() {
return $this->defaults;
}
/**
* @param string $method
*/
public function setMethod($method) {
$this->method = $method;
}
/**
* @return string
*/
public function getMethod() {
return $this->method;
}
/**
* @param array $params
*/
public function setParams($params) {
$this->params=$params;
}
/**
* @return array
*/
public function getParams() {
return $this->params;
}
/**
* @param string $path
*/
public function setPath($path) {
$this->path = HTTP_SERVER.$path;
}
/**
* @return string
*/
public function getPath() {
return $this->path;
}
/**
* Generuje przyjazny link.
* @param array $data
* @return string
*/
public function generateUrl($data) {
foreach($key_data as $key) {
$data2['<'.$key.'>']=$data[$key];
}
}
}
RouteCollection
<?php
namespace Avello\
System\Engine\Router
;
/**
* Klasa zawiera kolekcję elementów klasy Route.
* @package Avello\System\Engine\Router
* @author Łukasz Socha <kontakt@lukasz-socha.pl>
* @version 1.0
*/
class RouteColletion {
/**
* @var array Tablica obiektów klasy Route
*/
protected $items;
/**
* Dodaje obgiekt Route do kolekcji
* @param string $name Nazwa elementu
* @param Route $item Obiekt Route
*/
public function add($name, $item) {
$this->items[$name]=$item;
}
public function get($name) {
return $this->items[$name];
} else {
return null;
}
}
/**
* Zwraca wszystkie obiekty kolekcji
* @return array array
*/
public function getAll() {
return $this->items;
}
}
Router:
<?php
namespace Avello\
System\Engine\Router
;
/**
* Klasa Routera.
* @package Avello\System\Engine\Router
* @author Łukasz Socha <kontakt@lukasz-socha.pl>
* @version 1.0
*/
class Router {
/**
* @var String URL do przetworzenia
*/
protected $url;
/**
* @var array Zawiera objekt RouteCollecion.
*/
protected $controller;
protected $class;
protected $method;
public function __construct($url, $collection=null) {
if($collection!=null) {
Router::$collection = $collection;
}
$this->url = $url;
}
/**
* @param array $collection
*/
public function setCollection($collection) {
Router::$collection = $collection;
}
/**
* @return array
*/
public function getCollection() {
return Router::$collection;
}
/**
* @param mixed $class
*/
public function setClass($class)
{
$this->class = $class;
}
/**
* @return mixed
*/
public function getClass()
{
return $this->class;
}
/**
* @param mixed $controller
*/
public function setController($controller)
{
$this->controller = $controller;
}
/**
* @return mixed
*/
public function getController()
{
return $this->controller;
}
/**
* @param mixed $method
*/
public function setMethod($method)
{
$this->method = $method;
}
/**
* @return mixed
*/
public function getMethod()
{
return $this->method;
}
/**
* @param String $url
*/
public function setUrl($url) {
$this->url = $url;
}
/**
* @return String
*/
public function getUrl() {
return $this->url;
}
protected function matchRoute($route) {
$url= $route->getPath();
//var_dump($this->url);
//var_dump($results);
if($results) {
$this->controller=$route->getController();
$this->class=$route->getClass();
$this->method=$route->getMethod();
return true;
}
return false;
}
public function run() {
foreach(Router::$collection->getAll() as $route) {
if($this->matchRoute($route)) {
return true;
}
}
return false;
}
}
Przykład
$collection = new \Avello\
System\Engine\Router\RouteColletion
();
$collection->add('demo', new \Avello\
System\Engine\Router\Route
( 'demo(/<id>)?',
'controller' => 'kontroler',
'method' => 'metoda'
),
'id' => '\d+'
),
'id' => 0
)
));
$collection->add('product_one', new \Avello\
System\Engine\Router\Route
( '<slug>?-p-<id>?',
'controller' => DIR_CATALOG.'controller/catalog/product.php',
'method' => 'index',
'class' => 'ControllerProductProduct'
),
'slug' => '\w+',
'id' => '\d+'
),
'slug' => '',
'id' => 0
)
));
$collection->add('cart', new \Avello\
System\Engine\Router\Route
( 'koszyk',
'controller' => DIR_CATALOG.'controller/checkout/cart.php',
'method' => 'index',
'class' => 'ControllerCheckoutCart'
)
));
$collection->add('login', new \Avello\
System\Engine\Router\Route
( 'logowanie',
'controller' => DIR_CATALOG.'controller/account/login.php',
'method' => 'index',
'class' => 'ControllerAccountLogin'
)
));
$router = new \Avello\
System\Engine\Router\Router
($_SERVER['REQUEST_URI'], $collection);