Adding _METHOD support to routing

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1210 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-02-27 15:36:49 +00:00
parent 6ce3776d12
commit a50e28dc16
2 changed files with 24 additions and 6 deletions

View File

@@ -91,15 +91,21 @@ This is the routing. You need to add the routes in your index.php file with :
$route->put ("home", function () { echo "YESY"; }); $route->put ("home", function () { echo "YESY"; });
$route->delete ("home", function () { echo "YESY"; }); $route->delete ("home", function () { echo "YESY"; });
The definition match on the HTTP method (GET,POST,PUT or DELETE). The definition match on the HTTP method (GET,POST,PUT or DELETE). It can be
surcharged by using the _METHOD variable in POST method, with the wanted method
to use.
The routes are read sequentially. The first which match the class/method wins. The routes are read sequentially. The first which match the class/method wins.
You should carrefully order the rules.
Add the debug on routing with
$route->debug=TRUE;
to help to see the matches.
We can use some variables to be passed to the called function : We can use some variables to be passed to the called function :
$route->get ("home3/{plo}/{str}", function ($str, $plo) {echo "$str $plo";}); $route->get ("home3/{plo}/{str}", function ($str, $plo) {echo "$str $plo";});
There is a special action named "redirect" in the "route" class. It provide the There is a special action named "redirect" in the "route" class. It provide the
capability to re-route the Web browser to another page. Put in action : capability to re-route the Web browser to another page. Put in action :
array ("route", "redirect", "nextPage/", FALSE), function () use ($route) {$route->redirect("/home4", FALSE);}
If the URL match the requested search, the Web browser will load immediately a If the URL match the requested search, the Web browser will load immediately a
new page named "nextPage/". The FALSE will be change in case of modular new page named "nextPage/". The FALSE will be change in case of modular
application (see the MODULAR APPLICATION chapter) application (see the MODULAR APPLICATION chapter)

View File

@@ -137,7 +137,8 @@ class route
- search can be the end of URL and optionnal parameters in {var1} format - search can be the end of URL and optionnal parameters in {var1} format
{var1} must be the name of the parameter of the method {var1} must be the name of the parameter of the method
- action is an array with (0=>CLASS, 1=>method, 2...=>parameters) */ - action is an array with (0=>CLASS, 1=>method, 2...=>parameters) */
function routingCollection ($routes) // DISABLED FUNCTION : TOO COMPLEX !!
/*function routingCollection ($routes)
{ {
if ($this->debug) if ($this->debug)
echo "<pre>==== DEBUG : ROUTING START ====\n"; echo "<pre>==== DEBUG : ROUTING START ====\n";
@@ -251,7 +252,7 @@ class route
echo "URL='";print_r ($url); echo "'\n"; echo "URL='";print_r ($url); echo "'\n";
echo "ROUTE='";print_r ($route); echo "'\n"; echo "ROUTE='";print_r ($route); echo "'\n";
}*/ }*/
/*
// URL === ROUTE (No variables) // URL === ROUTE (No variables)
if ($url === $route) if ($url === $route)
@@ -388,12 +389,23 @@ class route
// TODO : ERROR 404 ! // TODO : ERROR 404 !
echo "404"; echo "404";
return; return;
} }*/
/** Return the HTTP method used to connect to the page /** Return the HTTP method used to connect to the page
Can be override by a _METHOD parameter provided in POST
Throw an exception in case of error */ Throw an exception in case of error */
public function method () public function method ()
{ {
if (isset ($_POST["_METHOD"]) &&
($_POST["_METHOD"] === "GET" ||
$_POST["_METHOD"] === "POST" ||
$_POST["_METHOD"] === "PUT" ||
$_POST["_METHOD"] === "DELETE"))
{
$this->method = $_POST["_METHOD"];
return $_POST["_METHOD"];
}
if (!isset ($_SERVER["REQUEST_METHOD"])) if (!isset ($_SERVER["REQUEST_METHOD"]))
throw new Exception ("No REQUEST_METHOD", 415); throw new Exception ("No REQUEST_METHOD", 415);
if ($_SERVER["REQUEST_METHOD"] === "GET" || if ($_SERVER["REQUEST_METHOD"] === "GET" ||
@@ -474,7 +486,7 @@ class route
{ {
if ($this->debug) if ($this->debug)
echo "<pre>==> DELETE $route ?? "; echo "<pre>==> DELETE $route ?? ";
if ($this->method () !== " DELETE") if ($this->method () !== "DELETE")
{ {
if ($this->debug) if ($this->debug)
echo "==> Not a DELETE Method\n"; echo "==> Not a DELETE Method\n";