Exemplo n.º 1
0
 /**
  * Runs the callback for the given request
  */
 public static function dispatch()
 {
     $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
     $method = $_SERVER['REQUEST_METHOD'];
     $searches = array_keys(static::$patterns);
     $replaces = array_values(static::$patterns);
     $found_route = false;
     self::$routes = str_replace('//', '/', self::$routes);
     // Check if route is defined without regex
     if (in_array($uri, self::$routes)) {
         $route_pos = array_keys(self::$routes, $uri);
         foreach ($route_pos as $route) {
             // Using an ANY option to match both GET and POST requests
             if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') {
                 $found_route = true;
                 // If route is not an object
                 if (!is_object(self::$callbacks[$route])) {
                     // Grab all parts based on a / separator
                     $parts = explode('/', self::$callbacks[$route]);
                     // Collect the last index of the array
                     $last = end($parts);
                     // Grab the controller name and method call
                     $segments = explode('@', $last);
                     // Instanitate controller
                     $controller = new $segments[0]();
                     // Call method
                     $controller->{$segments}[1]();
                     if (self::$halts) {
                         return;
                     }
                 } else {
                     // Call closure
                     call_user_func(self::$callbacks[$route]);
                     if (self::$halts) {
                         return;
                     }
                 }
             }
         }
     } else {
         // Check if defined with regex
         $pos = 0;
         foreach (self::$routes as $route) {
             if (strpos($route, ':') !== false) {
                 $route = str_replace($searches, $replaces, $route);
             }
             if (preg_match('#^' . $route . '$#', $uri, $matched)) {
                 if (self::$methods[$pos] == $method) {
                     $found_route = true;
                     // Remove $matched[0] as [1] is the first parameter.
                     array_shift($matched);
                     if (!is_object(self::$callbacks[$pos])) {
                         // Grab all parts based on a / separator
                         $parts = explode('/', self::$callbacks[$pos]);
                         // Collect the last index of the array
                         $last = end($parts);
                         // Grab the controller name and method call
                         $segments = explode('@', $last);
                         // Instanitate controller
                         $controller = new $segments[0]();
                         // Fix multi parameters
                         if (!method_exists($controller, $segments[1])) {
                             echo "controller and action not found";
                         } else {
                             call_user_func_array(array($controller, $segments[1]), $matched);
                         }
                         if (self::$halts) {
                             return;
                         }
                     } else {
                         call_user_func_array(self::$callbacks[$pos], $matched);
                         if (self::$halts) {
                             return;
                         }
                     }
                 }
             }
             $pos++;
         }
     }
     // Run the error callback if the route was not found
     if ($found_route == false) {
         if (!self::$error_callback) {
             self::$error_callback = function () {
                 header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found");
                 echo '404';
             };
         }
         call_user_func(self::$error_callback);
     }
 }
Exemplo n.º 2
0
 /**
  * Runs the callback for the given request
  */
 public static function dispatch()
 {
     $uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
     if (isset($_REQUEST['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
         $_SERVER['REQUEST_METHOD'] = strtoupper($_REQUEST['HTTP_X_HTTP_METHOD_OVERRIDE']);
     }
     $method = $_SERVER['REQUEST_METHOD'];
     $searches = array_keys(static::$patterns);
     $replaces = array_values(static::$patterns);
     $found_route = false;
     foreach (self::$filters_callbacks['before'] as $filter => $callbacks) {
         //echo $uri.'---'.$filter.'==='.strpos($uri,$filter).'---';
         if (strpos($uri, $filter) !== false) {
             foreach ($callbacks as $callback) {
                 if (!is_object($callback)) {
                     $segments = explode('@', $callback);
                     $controller = new $segments[0]();
                     self::$halts = $controller->{$segments}[1];
                     if (self::$halts) {
                         return;
                     }
                 } else {
                     self::$halts = call_user_func($callback);
                     if (self::$halts) {
                         return;
                     }
                 }
             }
         }
     }
     //echo $uri;
     // check if route is defined without regex
     if (isset(self::$routes[$method][$uri])) {
         $found_route = true;
         //if route is not an object
         if (!is_object(self::$routes[$method][$uri])) {
             $namespace = '';
             if (is_array(self::$routes[$method][$uri])) {
                 $callback = self::$routes[$method][$uri]['uses'];
                 //use namespace
                 if (isset(self::$routes[$method][$uri]['namespace'])) {
                     $namespace = trim(self::$routes[$method][$uri]['namespace'], '\\') . '\\';
                 }
             } else {
                 $callback = self::$routes[$method][$uri];
             }
             //grab all parts based on a / separator
             $parts = explode('/', $callback);
             //collect the last index of the array
             $last = end($parts);
             //grab the controller name and method call
             $segments = explode('@', $last);
             //instanitate controller
             $class = $namespace . $segments[0];
             $controller = new $class();
             //call method
             $controller->{$segments}[1]();
             if (self::$halts) {
                 return;
             }
         } else {
             //call closure
             call_user_func(self::$routes[$method][$uri]);
             if (self::$halts) {
                 return;
             }
         }
     } else {
         // check if defined with regex
         $pos = 0;
         if (isset(self::$routes[$method])) {
             foreach (self::$routes[$method] as $route => $callback) {
                 if (strpos($route, ':') !== false) {
                     $route = str_replace($searches, $replaces, $route);
                 }
                 if (preg_match('#^' . $route . '$#', $uri, $matched)) {
                     $found_route = true;
                     array_shift($matched);
                     //remove $matched[0] as [1] is the first parameter.
                     if (!is_object($callback)) {
                         $namespace = '';
                         if (is_array($callback)) {
                             $callback = $callback['uses'];
                             //use namespace
                             if (isset($callback['namespace'])) {
                                 $namespace = trim($callback['namespace'], '\\') . '\\';
                             }
                         }
                         //grab all parts based on a / separator
                         $parts = explode('/', $callback);
                         //collect the last index of the array
                         $last = end($parts);
                         //grab the controller name and method call
                         $segments = explode('@', $last);
                         //instanitate controller
                         $controller = new $segments[0]();
                         //call method and pass any extra parameters to the method
                         call_user_func_array(array($controller, $segments[1]), $matched);
                         //$controller->$segments[1](implode(",", $matched));
                         if (self::$halts) {
                             return;
                         }
                     } else {
                         call_user_func_array($callback, $matched);
                         if (self::$halts) {
                             return;
                         }
                     }
                 }
             }
         }
     }
     // run the error callback if the route was not found
     if ($found_route == false) {
         if (!self::$error_callback) {
             self::$error_callback = function () {
                 header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found");
                 die('404');
             };
         }
         call_user_func(self::$error_callback);
     }
     foreach (self::$filters_callbacks['after'] as $filter => $callbacks) {
         if (strpos($uri, $filter) !== false) {
             foreach ($callbacks as $callback) {
                 if (!is_object($callback)) {
                     $segments = explode('@', $callback);
                     $controller = new $segments[0]();
                     $controller->{$segments}[1];
                     if (self::$halts) {
                         return;
                     }
                 } else {
                     self::$halts = call_user_func($callback);
                     if (self::$halts) {
                         return;
                     }
                 }
             }
         }
     }
 }