Napisałem prosty Page Controller(jeśli można to tak nazwać) - namespace->class->method. Zastanawiam się nad parametrami metody przekazywanymi w url przy użyciu Reflection Api - mam na myśli akcje w postaci:
/user/show/{id_usera} - które trzeba skądś wziąć
Nie wiem, czy to aby mądre(bezpieczne), czy też nie można by zrobić tego inaczej - łatwiej.
Ogólnie rzecz biorąc proszę też o ocenę całości, jakąś podpowiedź, co mogę zmienić, dodać, czy też całość jest nie tak i naprowadzenie na jakąś inną koncepcję.
PageController:
<?php class PageController extends AbstractController { /** * @var array * @Description : */ private $path = []; /** * @var array * @Description : store function class parameters if exist */ private $paramters = []; /** * @Constructor : PageController * @param : $namespace * @Description : collect the address path from @Class Request * induction @Method init to create the path */ public function __construct($namespace) { $path = new Request(); $this->init($path, $namespace); } /** * @Method : init * @param Request : $address - initializes address path * @param : $namespace - namespace of class which will be including * @Description : create controller path and his action + params if exists * induction @Method doIfExist for the action */ private function init(Request $address, $namespace) { $this->path = $address->getAll(); $namespace = filter_var($namespace, FILTER_SANITIZE_STRING); } $controller = $namespace.$this->path[0]; if ($quantity > 2) { for ($i=2; $i<$quantity; $i++) { $this->paramters[] = $this->path[$i]; } $this->doIfExist($controller, $action, $this->paramters); } else { $this->doIfExist($controller, $action); } } /** * @Method : doIfExist * @param : $controller - class path with namespace * @param : $action - class action * @param : null $other - action parameters * @Description : check that @Class $controller and his @Method $action are exist * ... */ private function doIfExist($controller, $action, $other=null) { if (class_exists($controller) && method_exists($controller, $action)) { $controller = new $controller(); // Odzielić $reflection = new \ReflectionClass($controller); $method = $reflection->getMethod($action); $params = $method->getParameters(); //dodać ilość parametrów $controller->$action($other); } else { $controller->$action(); } } else { self::error404(); } } } // uzycie new PageController('namespace/');
Request:
<?php class Request { /** * @const : string * @Description : store default application path */ const DEFAULT_PATH = 'index'; /** * @var : array * @Description : store address elements from PATH_INFO */ private $feedback = []; /** * @Constructor : Request * @Arguments : none * @Description : induction @Method init */ public function __construct() { $this->init(); } /** * @Method : init * @Arguments : none * @Description : Function which initialize address table @var $feedback */ private function init() { for ($i=0; $i<$count; $i++) { $this->feedback[$i] = filter_var($this->feedback[$i], FILTER_SANITIZE_STRING); } } else { $this->setBasic(); } } /** * @Method : setBasic * @Arguments : none * @Description : Function to set default path(class and method), if PATH_INFO does not exist */ private function setBasic() { } } /** * @Method : getAll * @Arguments : none * @Description : Function return address path @var $feedback * @return array */ public function getAll() { return $this->feedback; } }
Pozdrawiam,
szubi