/**
  * @param $url
  * @throws Exception
  * Метод для парсинга урла
  */
 public static function parse($url)
 {
     $arr = explode('?', $url);
     $url = rtrim($arr[0], '/');
     // если пусто - то на главную
     if (!$url) {
         self::$controller = 'IndexController';
         self::$action = 'indexAction';
         return;
     }
     // перебор роутов на предмет совпадения решулярки с урлом
     foreach (self::$routes as $route => $item) {
         $regex = $item['pattern'];
         foreach ($item['params'] as $k => $v) {
             $regex = str_replace('{' . $k . '}', '(' . $v . ')', $regex);
         }
         // если совпало
         if (preg_match('@^' . $regex . '$@', $url, $matches)) {
             array_shift($matches);
             if ($matches) {
                 foreach ($item as $k1 => $v1) {
                     $matches = array_combine(array_keys($item['params']), $matches);
                 }
             }
             // определяем названия класса/метода контроллера
             self::$controller = $item['controller'] . 'Controller';
             self::$action = $item['action'] . 'Action';
             $_GET = $matches + $_GET;
             break;
         }
     }
     if (is_null(self::$controller) || is_null(self::$action)) {
         throw new Exception('Page not found', 404);
     }
 }
示例#2
0
文件: Router.php 项目: Ro911/MVC
 /**
  * @param $url
  * @throws Exception
  */
 public static function parse($url)
 {
     $arr = explode('?', $url);
     $url = rtrim($arr[0], '/');
     if (!$url) {
         self::$controller = 'IndexController';
         self::$action = 'indexAction';
         return;
     }
     foreach (self::$routes as $route => $item) {
         $regex = $item['pattern'];
         foreach ($item['params'] as $k => $v) {
             $regex = str_replace('{' . $k . '}', '(' . $v . ')', $regex);
         }
         if (preg_match('@^' . $regex . '$@', $url, $matches)) {
             array_shift($matches);
             if ($matches) {
                 foreach ($item as $k1 => $v1) {
                     $matches = array_combine(array_keys($item['params']), $matches);
                 }
             }
             self::$controller = $item['controller'] . 'Controller';
             self::$action = $item['action'] . 'Action';
             $_GET = $matches + $_GET;
             break;
         }
     }
     if (is_null(self::$controller) || is_null(self::$action)) {
         throw new Exception('Page not found', 404);
     }
 }
示例#3
0
文件: router.php 项目: nemis/Fm
 static function route_controller($applicationName)
 {
     $url = parse_url($_SERVER['REQUEST_URI']);
     $path = explode('/', trim($url['path'], '/'));
     if (!isset($path[1])) {
         self::$controller = 'index';
         self::$action = 'index';
     } else {
         array_shift($path);
         $dir = $applicationName . '/controllers/';
         foreach ($path as $p) {
             if (!is_dir($dir . $p . '/')) {
                 break;
             } else {
                 $dir .= $p . '/';
                 array_shift($path);
             }
         }
         self::$dir = $dir;
         //if (isset($path[0]) and $path[0] == 'index.php')
         self::$controller = array_shift($path);
         self::$action = array_shift($path);
         if (empty(self::$action)) {
             self::$action = 'index';
         }
     }
     foreach ($path as $k => $v) {
         self::$params[$k] = $v;
     }
     return array(self::$controller, self::$action, self::$params);
 }
示例#4
0
 public function getAction()
 {
     if (isset(self::$segments[1])) {
         self::$action = self::$segments[1];
     } else {
         self::$action = 'default';
     }
 }
示例#5
0
 public static function pareUrl()
 {
     if (isset($_GET['r'])) {
         $query_string = $_GET['r'];
         $query_string = explode('/', $query_string);
         self::$controller = $query_string[0];
         self::$action = $query_string[1];
     }
 }
示例#6
0
 public static function setAction($str)
 {
     self::$action = $str;
 }
示例#7
0
 /**
  * Определение запрашиваемого модуля и сегмента
  */
 public static function route()
 {
     self::parse_query();
     if (self::_check_segments()) {
         self::$segment1 = self::$parse['segment1'];
         self::$segment2 = self::$parse['segment2'];
         self::$segment3 = self::$parse['segment3'];
         if (empty(self::$segment1)) {
             if (file_exists(ROOT . 'modules/index/execute/index.php')) {
                 self::$controller_path = 'modules/index/execute/index.php';
                 self::$controller_name = 'index';
                 self::$action = '';
                 self::$page = '';
                 self::$module = 'index';
             } else {
                 self::$controller_exists = FALSE;
             }
         } elseif (!empty(self::$segment1) && !empty(self::$segment2)) {
             if (file_exists(ROOT . 'modules/' . self::$segment1 . '/execute/' . self::$segment1 . '.php')) {
                 self::$controller_path = 'modules/' . self::$segment1 . '/execute/' . self::$segment1 . '.php';
                 self::$controller_name = self::$segment1;
                 self::$action = self::$segment2;
                 self::$page = '';
                 self::$module = self::$segment1;
             } else {
                 self::$controller_exists = FALSE;
             }
         } elseif (!empty(self::$segment1)) {
             if (file_exists(ROOT . 'modules/' . self::$segment1 . '/execute/' . self::$segment1 . '.php')) {
                 self::$controller_path = 'modules/' . self::$segment1 . '/execute/' . self::$segment1 . '.php';
                 self::$controller_name = self::$segment1;
                 self::$action = '';
                 self::$page = '';
                 self::$module = self::$segment1;
             } else {
                 self::$controller_exists = FALSE;
             }
         } else {
             throw new Exception('00404');
         }
     }
     if (self::$controller_exists) {
         define('ROUTE_CONTROLLER_PATH', self::$controller_path);
         define('ROUTE_CONTROLLER_NAME', self::$controller_name);
         define('ROUTE_CONTROLLER_URL', self::$module . (self::$action ? '/' . self::$action : '') . (self::$page ? '/' . self::$page : ''));
         define('ROUTE_ACTION', self::$action);
         define('ROUTE_PAGE', self::$page);
         define('ROUTE_MODULE', self::$module);
     } elseif (!empty(self::$segment1)) {
         if (!empty(self::$segment2)) {
             if (is_numeric(self::$segment2)) {
                 if (!empty(self::$segment3)) {
                     self::$action = self::$segment3;
                 } else {
                     self::$action = "showThread";
                 }
             } else {
                 self::$action = self::$segment2;
             }
         } else {
             self::$action = "showBoard";
         }
         define('ROUTE_CONTROLLER_PATH', ROOT . 'modules/index/execute/index.php');
         define('ROUTE_CONTROLLER_NAME', 'index');
         define('ROUTE_CONTROLLER_URL', self::$segment1);
         define('ROUTE_ACTION', self::$action);
         define('ROUTE_SEGMENT', self::$segment2);
         define('ROUTE_PAGE', self::$segment3);
         define('ROUTE_MODULE', 'index');
         define('ROUTE_LOGIN', self::$segment1);
     } else {
         throw new Exception('00404');
     }
     //;
 }
示例#8
0
 private function _init()
 {
     $route = $this->_set_route();
     $route = substr($route, -1, 1) == "/" ? substr($route, 0, -1) : $route;
     $route = $route ? explode("/", $route) : null;
     // define the controller directory
     if (isset(self::$config_route['controller_dir_in_route']) && self::$config_route['controller_dir_in_route'] === true) {
         if (is_array($route) && count($route)) {
             self::$controller_dir = array_shift($route);
         } elseif (isset(self::$config_route["default_controller_dir"])) {
             self::$config_route["default_controller_dir"];
         } else {
             trigger_error("ROUTER: DEFAULT CONTROLLER DIR NOT SET");
         }
     }
     // define the controller
     if (is_array($route) && count($route)) {
         self::$controller = array_shift($route);
     } elseif (isset(self::$config_route['default_controller'])) {
         self::$controller = self::$config_route["default_controller"];
     } else {
         trigger_error("ROUTER: DEFAULT CONTROLLER NOT SET");
     }
     // define action
     if (is_array($route) && count($route)) {
         self::$action = array_shift($route);
     } elseif (isset(self::$config_route['default_action'])) {
         self::$action = self::$config_route['default_action'];
     } else {
         trigger_error("ROUTER: DEFAULT ACTION NOT SET");
     }
     // define the parameters
     if ($route) {
         self::$params = $route;
     }
 }
 public static function init()
 {
     $url = $_SERVER['REQUEST_URI'];
     $isCustom = false;
     if (count(self::$rules)) {
         foreach (self::$rules as $ruleKey => $ruleData) {
             $params = self::ruleMatch($ruleKey, $url);
             if ($params) {
                 self::$controller = $ruleData['controller'];
                 self::$action = $ruleData['action'];
                 self::$params = $params;
                 $isCustom = true;
                 break;
             }
         }
     }
     if (!$isCustom) {
         self::defaultRoutes($url);
     }
     if (!strlen(self::$controller)) {
         self::$controller = 'home';
     }
     if (!strlen(self::$action)) {
         self::$action = 'index';
     }
 }
示例#10
0
 public static function parse($uri)
 {
     self::$action = Config::get('default_action');
     self::$controller = Config::get('default_controller');
     self::$language = Config::get('default_language');
     self::$id = Config::get('default_id');
     $uri_elements = self::url_to_array($uri);
     if (count($uri_elements)) {
         //if (strtolower(current($uri_elements) != 'admiin')) {
         if (in_array(strtolower(current($uri_elements)), Config::get('languages'))) {
             self::$language = strtolower(current($uri_elements));
             array_shift($uri_elements);
         }
         //}else {
         //  array_shift($uri_elements);
         //}
         $url = implode('/', $uri_elements);
         self::find_alias($url);
         /**
                     if(current($uri_elements)){
                     self::$controller = ucfirst(strtolower(current($uri_elements)));
                     array_shift($uri_elements);
                     }
                     if(current($uri_elements)){
                     self::$action = strtolower(current($uri_elements));
                     array_shift($uri_elements);
                     }
                     if(current($uri_elements)){
                     self::$params = $uri_elements;
         
                     }
                      **/
     }
 }