в общем не претендую на оригинальность просто вдруг кому будет интересно
данный код взял с какогото урока или книги и чуть переделал
что бы работал так controller/metod/param1/...../pa ramN
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- <?PHP
- class Router{
- protected $_controller;
- protected $_action;
- protected $_params;
- static $_instance;
- public static function getInstance(){
- if(!(self::$_instance instanceOf self ))
- self::$_instance = new self();
- return self::$_instance;
- }
- private function __construct(){
- $request = $_SERVER['REQUEST_URI'];
- //conrtoller
- //ucfirst($splits[1])
- //method
- //params
- $value[] = $splits[$i];
- $this->_params = $value;
- }
- }
- public function route(){
- $rc = new ReflectionClass($this->getController());
- $rm = new ReflectionMethod($this->getController(), $this->getAction());
- if($rc->hasMethod($this->getAction())){
- $controller = $rc->newInstance();
- $method = $rc->getMethod($this->getAction());
- /***********************************
- if($method->isStatic())
- $method->invoke(null);
- else
- // *********************************/
- $par = $rm->getParameters();
- if($par && $this->getParams()!=0){
- $rm->invokeArgs($controller, $this->getParams());
- }//else { throw new Exception("Params"); } */
- $method->invoke($controller);
- } else { throw new Exception("Action"); }
- } else { throw new Exception("Interface");}
- } else { throw new Exception("Controller");}
- }
- function getController(){
- return $this->_controller;
- }
- function getAction(){
- return $this->_action;
- }
- function getParams(){
- return $this->_params;
- }
- }
- ?>