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:
16
docs/USAGE
16
docs/USAGE
@@ -86,16 +86,16 @@ you will allow the class "blog" to be launch with the parameter "page".
|
||||
This is the routing. You need to add the routes in your index.php file with :
|
||||
require_once ("domframework/route.php");
|
||||
$route = new route ();
|
||||
$res = $route->routingCollection (array (
|
||||
"blog/{page}" => array ("class", "method", "{page}"),
|
||||
));
|
||||
$route->get ("home", function () { echo "YESY"; });
|
||||
$route->post ("home", function () { echo "YESY"; });
|
||||
$route->put ("home", function () { echo "YESY"; });
|
||||
$route->delete ("home", function () { echo "YESY"; });
|
||||
|
||||
The definition match on the HTTP method (GET,POST,PUT or DELETE).
|
||||
The routes are read sequentially. The first which match the class/method wins.
|
||||
It start a new instance of "class" and apply the method "method" with the
|
||||
parameters need to launch the action. The result of the action is returned to
|
||||
the main script to be outputed.
|
||||
The parameters are puts between {}. You must put the parameters in the right
|
||||
order needed by your method.
|
||||
|
||||
We can use some variables to be passed to the called function :
|
||||
$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
|
||||
capability to re-route the Web browser to another page. Put in action :
|
||||
|
||||
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