Add a new routing module with easier support. It understand the RESTful mode too with GET,PUT,POST,DELETE.
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1209 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
133
route.php
133
route.php
@@ -4,6 +4,7 @@ class route
|
||||
{
|
||||
private $baseURL = "";
|
||||
private $baseURLmodule = "";
|
||||
public $method = "";
|
||||
public $module = NULL;
|
||||
public $debug=0; // 0:NoDebug, 1:routing, 2:more debug (developpement)
|
||||
//public $defaultOutput = "html"; // Default renderer : html
|
||||
@@ -388,4 +389,136 @@ class route
|
||||
echo "404";
|
||||
return;
|
||||
}
|
||||
|
||||
/** Return the HTTP method used to connect to the page
|
||||
Throw an exception in case of error */
|
||||
public function method ()
|
||||
{
|
||||
if (!isset ($_SERVER["REQUEST_METHOD"]))
|
||||
throw new Exception ("No REQUEST_METHOD", 415);
|
||||
if ($_SERVER["REQUEST_METHOD"] === "GET" ||
|
||||
$_SERVER["REQUEST_METHOD"] === "POST" ||
|
||||
$_SERVER["REQUEST_METHOD"] === "PUT" ||
|
||||
$_SERVER["REQUEST_METHOD"] === "DELETE")
|
||||
{
|
||||
$this->method = $_SERVER["REQUEST_METHOD"];
|
||||
return $_SERVER["REQUEST_METHOD"];
|
||||
}
|
||||
|
||||
throw new Exception ("Invalid REQUEST_METHOD", 406);
|
||||
}
|
||||
|
||||
/** If the URL is corresponding with $url, and the method is GET, then the
|
||||
function is called.
|
||||
Ex. : $app->get('/hello/{name}', function ($name) {
|
||||
echo "Hello, $name";
|
||||
}); */
|
||||
public function get ($route, $function)
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "<pre>==> GET $route ?? ";
|
||||
if ($this->method () !== "GET")
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "==> Not a GET Method\n";
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->map ($route, $function);
|
||||
}
|
||||
|
||||
/** If the URL is corresponding with $url, and the method is POST, then the
|
||||
function is called.
|
||||
Ex. : $app->get('/hello/{name}', function ($name) {
|
||||
echo "Hello, $name";
|
||||
}); */
|
||||
public function post ($route, $function)
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "<pre>==> POST $route ?? ";
|
||||
if ($this->method () !== "POST")
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "==> Not a POST Method\n";
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->map ($route, $function);
|
||||
}
|
||||
|
||||
/** If the URL is corresponding with $url, and the method is PUT, then the
|
||||
function is called.
|
||||
Ex. : $app->get('/hello/{name}', function ($name) {
|
||||
echo "Hello, $name";
|
||||
}); */
|
||||
public function put ($route, $function)
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "<pre>==> PUT $route ?? ";
|
||||
if ($this->method () !== "PUT")
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "==> Not a PUT Method\n";
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->map ($route, $function);
|
||||
}
|
||||
|
||||
/** If the URL is corresponding with $url, and the method is DELETE, then the
|
||||
function is called.
|
||||
Ex. : $app->get('/hello/{name}', function ($name) {
|
||||
echo "Hello, $name";
|
||||
}); */
|
||||
public function delete ($route, $function)
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "<pre>==> DELETE $route ?? ";
|
||||
if ($this->method () !== " DELETE")
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "==> Not a DELETE Method\n";
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->map ($route, $function);
|
||||
}
|
||||
|
||||
/** Do the mapping between the url and the route : call the function if
|
||||
thereis a match */
|
||||
public function map ($route, $function)
|
||||
{
|
||||
$url = substr ($this->requestURL (), strlen ($this->baseURLmodule ()));
|
||||
if ($this->debug)
|
||||
echo "$url ";
|
||||
if ($url === $route)
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "==> FOUND EQUAL !\n";
|
||||
$function ();
|
||||
exit;
|
||||
}
|
||||
|
||||
// URL === REGEXP ROUTE
|
||||
// Variables are exposed in url/{var1}/{var2}
|
||||
$regex = "#^$route$#U";
|
||||
$regex = str_replace ("{", "(?P<", $regex);
|
||||
$regex = str_replace ("}", ">.+)", $regex);
|
||||
unset ($matches);
|
||||
$rcRegex = preg_match ($regex, $url, $matches);
|
||||
if ($rcRegex !== FALSE && $rcRegex !== 0)
|
||||
{
|
||||
if ($this->debug)
|
||||
echo "==> FOUND REGEX !\n";
|
||||
$params = array ();
|
||||
$reflect = new ReflectionFunction ($function);
|
||||
foreach ($reflect->getParameters() as $key=>$val)
|
||||
$params[] = $matches[$val->name];
|
||||
call_user_func_array ($function, $params);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($this->debug)
|
||||
echo "==> NO MATCH\n";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user