Chciałbym stworzyć routing, do własnej implementacji MVC. Na początku przedstawię strukturę katalogów mojego projektu.

W folderze app, będą pliki danego projektu. Każdy contoller będzie miał swój plik .yml w którym będą zapisane zasady routingu.
przykładowy plik index.yml
prefiks oznacza wpisany adres url, contoller i action wiadomo.
index: prefix: /index/index/ controller: Index action: index show: prefix: /index/show/ controller: Index action: show add: prefix: /index/add/ controller: Index action: add
i tak działa mój routing:
1. ktoś wpisuje adres na mojej stronie np. index/show/40
2. sprawdzane jest czy istnieje dany controller o nazwie index, akcja show i parametr 40
3. jesli tak to uruchamiany jest odpowiednia akcja z contollera
Routing.php
class Routing { private $action; private $controller; private $parameter; private $routingFile; function __construct() { $this->createPath(); $this->loadRoutingFile($this->path['contoller']); $path = $this->searchRouting($this->path['contoller'] . '/' . $this->path['action'], $this-> routingFile); $this->setAction($path); $this->setContoller($path); } private function searchRouting($search, $array) { $array = (object)($array); foreach ($array as $routing) { if ($r == $search) { return $routing; } } return false; } private function loadRoutingFile($name) { require_once ('/libs/yaml/Yaml.php'); $this->routingFile = Yaml::parse('c:/wamp/www/framework/app/routing/' . $name . '.yml'); } else { $this->routingFile = Yaml::parse('c:/wamp/www/framework/app/routing/index.yml'); } } private function createPath() { $url[0] = 'index'; } if ($url[0] == "") { $url[0] = 'index'; } $url[1] = 'index'; } if ($url[1] == "") { $url[1] = 'index'; } $this->parameter=$url[2]; } $this->path['contoller'] = $url[0]; $this->path['action']= $url[1]; } public function getAction() { return $this->action; } public function getContoller() { return $this->controller; } public function getParameter() { return $this->parameter; } public function setAction($path) { $this->action = $path['action']; } public function setContoller($path) { $this->controller = $path['controller']; } }
Bootstrap.php
require_once 'core/Routing.php'; class Bootstrap { public function __construct(){ $routing=new Routing(); require_once('/app/controllers/'.$routing->getContoller().'Controller.php'); $contoller=$routing->getContoller().'Controller'; $action=$routing->getAction().'Action'; $page=new $contoller(); $page->$action($routing->getParameter()); } }
Chciałbym aby mój kod był uniwersalny i był elastyczny dla wielu różnych projektów. Chciałbym też aby został napisany o dobre praktyki OOP. Głównie to właśnie zależy mi na wysokiej jakości kodu. Co myślicie o takim rozwiązaniu tego zagadnienia? Wszystko działa w tym routingu. Jednak bardzo mi zależy aby pisać przemyślany kod. Gdzie popełniłem jakiś błąd projektując ten routing ?