コード例 #1
0
ファイル: Core.class.php プロジェクト: ToToSe/MyFramework
 private function routing()
 {
     $url = explode('/', $_SERVER['REQUEST_URI']);
     array_shift($url);
     array_shift($url);
     $urlStr = implode('/', $url);
     $reqRouting = self::$_pdo->query('SELECT * FROM routing WHERE url = "' . $urlStr . '"')->fetch();
     if ($reqRouting) {
         $reqRouting = explode('/', $reqRouting['real_path']);
         $action = end($reqRouting);
         $controller = $reqRouting[0];
     } else {
         $action = end($url);
         $controller = $url[0];
     }
     $callController = __NAMESPACE__ . '\\' . ucfirst($controller) . 'Controller';
     if (class_exists($callController)) {
         $o = new $callController();
         if (!method_exists($o, $action . 'Action')) {
             $action = 'default';
         }
     } else {
         $controller = 'default';
         $callController = __NAMESPACE__ . '\\' . ucfirst($controller) . 'Controller';
         $o = new $callController();
         if (!method_exists($o, $action . 'Action')) {
             $action = 'default';
         }
     }
     self::$_routing = ['controller' => $controller, 'action' => $action];
     return $o;
 }
コード例 #2
0
 /**
  * Fonction routing()
  * Gère le routage static et dynamique du framework
  * @return self::$_render;
  */
 private function routing()
 {
     self::$_routing = ['controller' => $this->controller, 'action' => $this->action];
     if ($_SERVER['REQUEST_URI']) {
         $instance = __NAMESPACE__ . '\\' . ucfirst($this->controller) . 'Controller';
         $obj = new $instance();
         $argUrl = array_filter(explode(DIRECTORY_SEPARATOR, $_SERVER['REQUEST_URI']));
         $nbArg = count($argUrl);
         // Si aucun argument après le nom du dirname
         if ($nbArg == 1) {
             $action = $this->action . "Action";
             $obj->{$action}();
         }
         if ($nbArg > 1) {
             // Le nom du premier argument en tant que controller
             $this->controller = $argUrl[2];
             if ($nbArg == 2 && $this->controller == 'connexion') {
                 if ($_SESSION['logged'] === true) {
                     header('Location: default/home');
                     exit;
                 }
                 $route = self::$_pdo->query("SELECT real_path FROM routing WHERE url = 'connexion'")->fetch();
                 $route = explode(DIRECTORY_SEPARATOR, $route['real_path']);
                 $this->action = $route[1];
                 $function = $this->action . 'Action';
             }
             $instance = __NAMESPACE__ . '\\' . ucfirst($this->controller) . 'Controller';
             $function = $this->action . 'Action';
             if (class_exists($instance)) {
                 $obj = new $instance();
                 // Si un controller et une action sont présents
                 if ($nbArg == 3) {
                     $this->action = $argUrl[3];
                     if ($this->action == 'connexion' && isset($_SESSION['logged']) && $_SESSION['logged'] === true) {
                         header('Location: default/home');
                         exit;
                     }
                     $function = $this->action . 'Action';
                 }
                 if (method_exists($obj, $function)) {
                     $obj->{$function}();
                 } else {
                     $function = $this->function;
                     $obj->{$function}();
                 }
             } elseif (isset($obj) && is_object($obj)) {
                 $obj->{$function}();
             } else {
                 self::$_render = 'Impossible de trouver la methode' . PHP_EOL;
             }
         }
         echo self::$_render;
     }
 }