diff --git a/route.php b/route.php index ae04f30..0f2aadc 100644 --- a/route.php +++ b/route.php @@ -305,7 +305,9 @@ class route ($_POST["_METHOD"] === "GET" || $_POST["_METHOD"] === "POST" || $_POST["_METHOD"] === "PUT" || - $_POST["_METHOD"] === "DELETE")) + $_POST["_METHOD"] === "DELETE" || + $_POST["_METHOD"] === "OPTIONS" + )) { $this->method = $_POST["_METHOD"]; return $_POST["_METHOD"]; @@ -316,7 +318,8 @@ class route if ($_SERVER["REQUEST_METHOD"] === "GET" || $_SERVER["REQUEST_METHOD"] === "POST" || $_SERVER["REQUEST_METHOD"] === "PUT" || - $_SERVER["REQUEST_METHOD"] === "DELETE") + $_SERVER["REQUEST_METHOD"] === "DELETE" || + $_SERVER["REQUEST_METHOD"] === "OPTIONS") { $this->method = $_SERVER["REQUEST_METHOD"]; return $_SERVER["REQUEST_METHOD"]; @@ -334,7 +337,7 @@ class route /** If the URL is corresponding with $url, and the method is GET, then the * function is called. - * Ex. : $app->get('/hello/{name}', function ($name) { + * Ex. : $route->get ('/hello/{name}', function ($name) { * echo "Hello, $name"; * }); * @param string $route Route to check with the URL @@ -359,7 +362,7 @@ class route /** If the URL is corresponding with $url, and the method is POST, then the * function is called. - * Ex. : $app->get('/hello/{name}', function ($name) { + * Ex. : $route->post ('/hello/{name}', function ($name) { * echo "Hello, $name"; * }); * @param string $route Route to check with the URL @@ -384,7 +387,7 @@ class route /** If the URL is corresponding with $url, and the method is PUT, then the * function is called. - * Ex. : $app->get('/hello/{name}', function ($name) { + * Ex. : $route->put ('/hello/{name}', function ($name) { * echo "Hello, $name"; * }); * @param string $route Route to check with the URL @@ -409,7 +412,7 @@ class route /** If the URL is corresponding with $url, and the method is DELETE, then the * function is called. - * Ex. : $app->get('/hello/{name}', function ($name) { + * Ex. : $route->delete ('/hello/{name}', function ($name) { * echo "Hello, $name"; * }); * @param string $route Route to check with the URL @@ -432,9 +435,34 @@ class route return $this->map ($route, $function); } + /** If the URL is corresponding with $url, and the method is OPTIONS, then the + * function is called. + * Ex. : $route->options ('/hello/{name}', function ($name) { + * echo "Hello, $name"; + * }); + * @param string $route Route to check with the URL + * @param callable $function Function to be executed if the route match + * @return Exit of the PHP after displaying the page if match, or return the + * route object to chain it whith the next test + */ + public function options ($route, $function) + { + $route = $this->preroute.$route; + if ($this->debug) + echo "
==> OPTIONS $route ?? ";
+    if ($this->method () !== "OPTIONS")
+    {
+      if ($this->debug)
+        echo "==> Not a OPTIONS Method\n";
+      return $this;
+    }
+
+    return $this->map ($route, $function);
+  }
+
   /** Allow multiple methods to execute the function if the route is
     * corresponding to the URL.
-    * Ex. : $app->multi("get,post,put,delete", '/hello/{name}',
+    * Ex. : $route->multi("get,post,put,delete,options", '/hello/{name}',
     *                   function ($name) {
     *         echo "Hello, $name";
     *       });