- Params renamed to Neuron
- add path atribute to Router req - add viewPath param to View
This commit is contained in:
		| @@ -78,7 +78,7 @@ class Router { | ||||
|   *     redirigidos a "https://ejemplo.com/duckbrain/docs". | ||||
|   */ | ||||
|   public static function redirect($uri) { | ||||
|     header('Location: '.self::baseURI().substr($uri,1)); | ||||
|     header('Location: '.static::baseURI().substr($uri,1)); | ||||
|   } | ||||
|    | ||||
|   /* | ||||
| @@ -86,12 +86,12 @@ class Router { | ||||
|   * Solo se puede usar un middleware a la vez. | ||||
|   */ | ||||
|   public static function middleware($middleware){ | ||||
|     if (!isset(self::$last)) return; | ||||
|     if (!isset(static::$last)) return; | ||||
|      | ||||
|     $method = self::$last[0]; | ||||
|     $index = self::$last[1]; | ||||
|     $method = static::$last[0]; | ||||
|     $index = static::$last[1]; | ||||
|      | ||||
|     self::$$method[$index]['middleware'] = 'Middlewares\\'.$middleware; | ||||
|     static::$$method[$index]['middleware'] = 'Middlewares\\'.$middleware; | ||||
|   } | ||||
|    | ||||
|   /* | ||||
| @@ -102,12 +102,13 @@ class Router { | ||||
|   *     json  - Donde se encuentran los valores JSON enviados en el body | ||||
|   * | ||||
|   */ | ||||
|   private static function params() { | ||||
|     $args             = (object) ''; | ||||
|     $args->get        = new Params($_GET); | ||||
|     $args->post       = new Params($_POST); | ||||
|     $args->json       = new Params(self::get_json()); | ||||
|     return $args; | ||||
|   private static function getReq() { | ||||
|     $req             = (object) ''; | ||||
|     $req->get        = new Neuron($_GET); | ||||
|     $req->post       = new Neuron($_POST); | ||||
|     $req->json       = new Neuron(static::get_json()); | ||||
|     $req->path       = static::URIPath(); | ||||
|     return $req; | ||||
|   } | ||||
|    | ||||
|   /* | ||||
| @@ -135,8 +136,8 @@ class Router { | ||||
|   *   Devuelve un enlace estático | ||||
|   */ | ||||
|   public static function get($path, $callback) { | ||||
|     self::$get[] = self::parse($path, $callback); | ||||
|     self::$last = ['get', count(self::$get)-1]; | ||||
|     static::$get[] = static::parse($path, $callback); | ||||
|     static::$last = ['get', count(static::$get)-1]; | ||||
|     return new static(); | ||||
|   } | ||||
|    | ||||
| @@ -153,14 +154,14 @@ class Router { | ||||
|   *   Devuelve un enlace estático | ||||
|   */ | ||||
|   public static function post($path, $callback) { | ||||
|     self::$post[] = self::parse($path, $callback); | ||||
|     self::$last   = ['post', count(self::$post)-1]; | ||||
|     static::$post[] = static::parse($path, $callback); | ||||
|     static::$last   = ['post', count(static::$post)-1]; | ||||
|     return new static(); | ||||
|   } | ||||
|    | ||||
|   public static function put($path, $callback) { | ||||
|     self::$put[]  = self::parse($path, $callback); | ||||
|     self::$last   = ['put', count(self::$put)-1]; | ||||
|     static::$put[]  = static::parse($path, $callback); | ||||
|     static::$last   = ['put', count(static::$put)-1]; | ||||
|     return new static(); | ||||
|   } | ||||
|    | ||||
| @@ -177,11 +178,20 @@ class Router { | ||||
|   *   Devuelve un enlace estático | ||||
|   */ | ||||
|   public static function delete($path, $callback) { | ||||
|     self::$delete[] = self::parse($path, $callback); | ||||
|     self::$last     = ['delete', count(self::$put)-1]; | ||||
|     static::$delete[] = static::parse($path, $callback); | ||||
|     static::$last     = ['delete', count(static::$put)-1]; | ||||
|     return new static(); | ||||
|   } | ||||
|    | ||||
|   /* | ||||
|   * Devuelve el URI path actual | ||||
|   */ | ||||
|    | ||||
|   public static function URIPath() { | ||||
|     return preg_replace('/'.preg_quote(static::baseURI(), '/').'/', | ||||
|             '/', strtok($_SERVER['REQUEST_URI'], '?'), 1); | ||||
|   } | ||||
|    | ||||
|   /* | ||||
|   * Aplica los routers. | ||||
|   *  | ||||
| @@ -220,28 +230,27 @@ class Router { | ||||
|   *       especificado anteriormente a la hora de recibirlos. | ||||
|   */ | ||||
|   public static function apply() { | ||||
|     $uri =  preg_replace('/'.preg_quote(self::baseURI(), '/').'/', | ||||
|             '/', strtok($_SERVER['REQUEST_URI'], '?'), 1); | ||||
|     $uri =  static::URIPath(); | ||||
|     $routers = []; | ||||
|     switch ($_SERVER['REQUEST_METHOD']){ // Según el método selecciona un arreglo de routers configurados | ||||
|       case 'POST': | ||||
|         $routers = self::$post; | ||||
|         $routers = static::$post; | ||||
|         break; | ||||
|       case 'PUT': | ||||
|         $routers = self::$put; | ||||
|         $routers = static::$put; | ||||
|         break; | ||||
|       case 'DELETE': | ||||
|         $routers = self::$delete; | ||||
|         $routers = static::$delete; | ||||
|         break; | ||||
|       default: | ||||
|         $routers = self::$get; | ||||
|         $routers = static::$get; | ||||
|         break; | ||||
|     } | ||||
|      | ||||
|     foreach ($routers as $router) { // revisa todos los routers para ver si coinciden con la URI actual | ||||
|       if (preg_match_all('/^'.$router['path'].'\/?$/si',$uri, $matches)){ | ||||
|         unset($matches[0]); | ||||
|         $args = self::params(); | ||||
|         $args = static::getReq(); | ||||
|          | ||||
|         if (isset($matches[1])) { // Caso 1 - Con pseudovariables por URI | ||||
|           $params = []; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user