route : update docComment
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2735 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
177
route.php
177
route.php
@@ -18,30 +18,30 @@ class route
|
||||
/** The module name */
|
||||
public $module = NULL;
|
||||
/** The debug mode :
|
||||
0:NoDebug, 1:routing, 2:more debug (developpement)*/
|
||||
* 0:NoDebug, 1:routing, 2:more debug (developpement)*/
|
||||
public $debug=0;
|
||||
//public $defaultOutput = "html"; // Default renderer : html
|
||||
|
||||
/** Allow slashes in the url when matching the regex */
|
||||
public $allowSlashes = true;
|
||||
// Provide the the class catch errors in routing.
|
||||
// Must be provided in an array(class,method);
|
||||
/** Provide the the class catch errors in routing.
|
||||
* Must be provided in an array(class,method); */
|
||||
public $errors = null;
|
||||
/** Preroute used in modules */
|
||||
public $preroute = "";
|
||||
/** Authentication URL used if a 401 error is raised. If none is defined,
|
||||
just display a "Unauthorized" message */
|
||||
* just display a "Unauthorized" message */
|
||||
public $authenticationURL = null;
|
||||
|
||||
// Ratelimit the errors in route.php to not allow the hackers to brute force
|
||||
// the backend. The objct can be put to null to disable the feature
|
||||
/** Ratelimit the errors in route.php to not allow the hackers to brute force
|
||||
* the backend. The objct can be put to null to disable the feature */
|
||||
public $ratelimiter = null;
|
||||
|
||||
/// RENDERER PART ///
|
||||
/** Output type to no previous catched renderer (allow : json, xml, txt html)
|
||||
*/
|
||||
public $output = "html";
|
||||
/** Title by default : space to be compatible with HTML5 */
|
||||
/** Title by default : space to be compatible with HTML5 */
|
||||
public $title = " ";
|
||||
/** Filename of class containing the presentation layer */
|
||||
public $viewClass = FALSE;
|
||||
@@ -65,8 +65,11 @@ class route
|
||||
}
|
||||
|
||||
/** Return the baseURL of the site
|
||||
Always finish with a slash
|
||||
@param string|null $module The module name (if thereis one) */
|
||||
* Always finish with a slash
|
||||
* @param string|null $module The module name (if thereis one)
|
||||
* @param bool|null $absolute Return the baseURL in absolute
|
||||
* @return string The URL base
|
||||
*/
|
||||
function baseURL ($module = FALSE, $absolute=false)
|
||||
{
|
||||
if ($this->module === NULL)
|
||||
@@ -123,9 +126,11 @@ class route
|
||||
}
|
||||
|
||||
/** Return the baseURL for a resource (add a index.php?url= if there is no
|
||||
mod_rewrite support. Used to link to modules in the HTML page.
|
||||
The baseURL is the real base of the project, which is different when not
|
||||
using the mod_rewrite. They are equal when using the mod_rewrite */
|
||||
* mod_rewrite support. Used to link to modules in the HTML page.
|
||||
* The baseURL is the real base of the project, which is different when not
|
||||
* using the mod_rewrite. They are equal when using the mod_rewrite
|
||||
* @return string The baseURL for the resource
|
||||
*/
|
||||
function baseURLresource ()
|
||||
{
|
||||
if ($this->baseURL === "")
|
||||
@@ -138,7 +143,9 @@ class route
|
||||
}
|
||||
|
||||
/** Return the baseURL of the module
|
||||
Always finish with a slash */
|
||||
* Always finish with a slash
|
||||
* @return string The baseURL for the module
|
||||
*/
|
||||
function baseURLmodule ()
|
||||
{
|
||||
if ($this->baseURLmodule !== "")
|
||||
@@ -148,13 +155,16 @@ class route
|
||||
}
|
||||
|
||||
/** Define the base URL of the site
|
||||
@param string $baseURL The base URL of the site */
|
||||
* @param string $baseURL The base URL of the site */
|
||||
function baseURLset ($baseURL)
|
||||
{
|
||||
$this->baseURL = $baseURL;
|
||||
}
|
||||
|
||||
/** Return the complete URL used to see this page */
|
||||
/** Return the complete URL used to see this page
|
||||
* @param bool|null $absolute Return the absolute URL
|
||||
* @return string the complete URL used to see this page
|
||||
*/
|
||||
function requestURL ($absolute = false)
|
||||
{
|
||||
$url = "";
|
||||
@@ -191,12 +201,14 @@ class route
|
||||
}
|
||||
|
||||
/** Do all the routing with redirections
|
||||
$destURL can be absolute or relative
|
||||
If module is set, the site is modular and a directory is named with module
|
||||
name
|
||||
@param string $destURL Do a redirection of the HTTP page
|
||||
@param string|null $module The module name
|
||||
@param boolean|null Permanent redirect (false by default) */
|
||||
* $destURL can be absolute or relative
|
||||
* If module is set, the site is modular and a directory is named with module
|
||||
* name
|
||||
* @param string $destURL Do a redirection of the HTTP page
|
||||
* @param string|null $module The module name
|
||||
* @param boolean|null Permanent redirect (false by default)
|
||||
* @return Exit of the PHP after doing the redirection
|
||||
*/
|
||||
function redirect ($destURL, $module="", $permanent = false)
|
||||
{
|
||||
if (php_sapi_name () === "cli")
|
||||
@@ -264,8 +276,9 @@ class route
|
||||
}
|
||||
|
||||
/** 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 */
|
||||
* Can be override by a _METHOD parameter provided in POST
|
||||
* @return string the method used to connect
|
||||
* @throws an exception in case of error */
|
||||
public function method ()
|
||||
{
|
||||
if (isset ($_POST["_METHOD"]) &&
|
||||
@@ -293,12 +306,15 @@ 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) {
|
||||
echo "Hello, $name";
|
||||
});
|
||||
@param string $route Route to check with the URL
|
||||
@param function $function Function to be executed if the route match */
|
||||
* function is called.
|
||||
* Ex. : $app->get('/hello/{name}', function ($name) {
|
||||
* echo "Hello, $name";
|
||||
* });
|
||||
* @param string $route Route to check with the URL
|
||||
* @param function $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 get ($route, $function)
|
||||
{
|
||||
$route = $this->preroute.$route;
|
||||
@@ -315,12 +331,15 @@ 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) {
|
||||
echo "Hello, $name";
|
||||
});
|
||||
@param string $route Route to check with the URL
|
||||
@param function $function Function to be executed if the route match */
|
||||
* function is called.
|
||||
* Ex. : $app->get('/hello/{name}', function ($name) {
|
||||
* echo "Hello, $name";
|
||||
* });
|
||||
* @param string $route Route to check with the URL
|
||||
* @param function $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 post ($route, $function)
|
||||
{
|
||||
$route = $this->preroute.$route;
|
||||
@@ -337,12 +356,15 @@ 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) {
|
||||
echo "Hello, $name";
|
||||
});
|
||||
@param string $route Route to check with the URL
|
||||
@param function $function Function to be executed if the route match */
|
||||
* function is called.
|
||||
* Ex. : $app->get('/hello/{name}', function ($name) {
|
||||
* echo "Hello, $name";
|
||||
* });
|
||||
* @param string $route Route to check with the URL
|
||||
* @param function $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 put ($route, $function)
|
||||
{
|
||||
$route = $this->preroute.$route;
|
||||
@@ -359,12 +381,15 @@ 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) {
|
||||
echo "Hello, $name";
|
||||
});
|
||||
@param string $route Route to check with the URL
|
||||
@param function $function Function to be executed if the route match */
|
||||
* function is called.
|
||||
* Ex. : $app->get('/hello/{name}', function ($name) {
|
||||
* echo "Hello, $name";
|
||||
* });
|
||||
* @param string $route Route to check with the URL
|
||||
* @param function $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 delete ($route, $function)
|
||||
{
|
||||
$route = $this->preroute.$route;
|
||||
@@ -381,13 +406,17 @@ class route
|
||||
}
|
||||
|
||||
/** Allow multiple methods to execute the function if the route is
|
||||
corresponding to the URL.
|
||||
Ex. : $app->multi("get,post,put,delete", '/hello/{name}', function ($name) {
|
||||
echo "Hello, $name";
|
||||
});
|
||||
@param string $methods The allowed methods, separated by commas (,)
|
||||
@param string $route Route to check with the URL
|
||||
@param function $function Function to be executed if the route match */
|
||||
* corresponding to the URL.
|
||||
* Ex. : $app->multi("get,post,put,delete", '/hello/{name}',
|
||||
* function ($name) {
|
||||
* echo "Hello, $name";
|
||||
* });
|
||||
* @param string $methods The allowed methods, separated by commas (,)
|
||||
* @param string $route Route to check with the URL
|
||||
* @param function $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 multi ($methods, $route, $function)
|
||||
{
|
||||
foreach (explode (",", $methods) as $method)
|
||||
@@ -398,9 +427,12 @@ class route
|
||||
}
|
||||
|
||||
/** Do the mapping between the url and the route : call the function if
|
||||
thereis a match
|
||||
@param string $route Route to check with the URL
|
||||
@param function $function Function to be executed if the route match */
|
||||
* thereis a match
|
||||
* @param string $route Route to check with the URL
|
||||
* @param function $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 map ($route, $function)
|
||||
{
|
||||
$url = str_replace ("index.php?url=", "", $this->requestURL ());
|
||||
@@ -433,25 +465,17 @@ class route
|
||||
$renderer->variable = $this->variable;
|
||||
$renderer->run ();
|
||||
exit;
|
||||
$class = "output".$this->output;
|
||||
require_once ("$class.php");
|
||||
$output = new $class ();
|
||||
$output->out ($data);
|
||||
exit;
|
||||
}
|
||||
|
||||
// URL === REGEXP ROUTE
|
||||
// Variables are exposed in url/{var1}/{var2}
|
||||
$regex = "#^$route$#U";
|
||||
//$regex = str_replace ("?", "\?", $regex);
|
||||
//$regex = str_replace (".", "\.", $regex);
|
||||
$regex = str_replace ("{", "(?P<", $regex);
|
||||
if ($this->allowSlashes)
|
||||
$regex = str_replace ("}", ">.+)", $regex);
|
||||
else
|
||||
$regex = str_replace ("}", ">[^/]+)", $regex);
|
||||
unset ($matches);
|
||||
//echo "REGEX=".htmlentities ($regex)." ? URL=".htmlentities ($url);
|
||||
$rcRegex = preg_match ($regex, $url, $matches);
|
||||
if ($rcRegex !== FALSE && $rcRegex !== 0)
|
||||
{
|
||||
@@ -490,11 +514,6 @@ class route
|
||||
$renderer->variable = $this->variable;
|
||||
$renderer->run ();
|
||||
exit;
|
||||
$class = "output".$this->output;
|
||||
require_once ("$class.php");
|
||||
$output = new $class ();
|
||||
$output->out ($data);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($this->debug)
|
||||
@@ -503,7 +522,9 @@ class route
|
||||
}
|
||||
|
||||
/** Print an error page. If a custom page exists, use it
|
||||
@param $e Exception to print */
|
||||
* @param $e Exception to print
|
||||
* @return bool true after the error is displayed
|
||||
*/
|
||||
public function error ($e)
|
||||
{
|
||||
$ipClient = null;
|
||||
@@ -596,14 +617,11 @@ class route
|
||||
$renderer->variable = $this->variable;
|
||||
$renderer->run ();
|
||||
return true;
|
||||
$class = "output".$this->output;
|
||||
require_once ("$class.php");
|
||||
$output = new $class ();
|
||||
$output->out ($message, $http->codetext ($getCode));
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Return the last valid get page to return */
|
||||
/** Return the last valid get page to return
|
||||
* @return The last valid page URL
|
||||
*/
|
||||
public function lastValidGetPage ()
|
||||
{
|
||||
if (isset ($_SESSION["domframework"]["route"]["lastGet"]))
|
||||
@@ -611,7 +629,10 @@ class route
|
||||
return "";
|
||||
}
|
||||
|
||||
/** Redirect to last valid get page if defined */
|
||||
/** Redirect to last valid get page if defined
|
||||
* @return Exit from PHP after redirect or null if the last valid page is not
|
||||
* defined
|
||||
*/
|
||||
public function lastValidGetPageRedirect ()
|
||||
{
|
||||
$lastValidGetPage = $this->lastValidGetPage ();
|
||||
|
||||
Reference in New Issue
Block a user