Back to the module list

Routing

The main routing is done in index.php file at the top directory of the project

Basic routing

The available methods are get, post, delete, put.

chdir (dirname (__FILE__));
$rc = include_once ("domframework/route.php");
if ($rc === false)
  die ("Please install the domframework !");

session_start ();

$route = new route ();
$route
// Use the GET method and without URL parameter
->get ("", function () use ($route)
{
  $route->redirect ("/ipsets", "");
})
// Use the GET method and with URL parameter "ipsets"
->get ("ipsets", function () use ($route)
{
  // List all the defined IPSets, with the menu to create/edit/delete one
  $route->viewMethod = "listing";
  $route->title = _("Defined ipsets");
  $ipsetsObj = new \controllers\ipsets ();
  return array ("ipsets"=>$ipsetsObj->listSets ());
})
// Use the POST method and with URL parameter "ipsets/add"
->post ("ipsets/add", function () use ($route)
{
  // Create really a new ipset
  $ipsetsObj = new \controllers\ipsets ();
  $form = new \form ();
  $values = $form->values ();
  $errors = $ipsetsObj->verify ($values);
  $form->saveValuesErrors ($values, $errors);
  if (count ($errors)) $route->redirect ("/ipsets/add");
  $ipsetsObj->createSet ($values["setname"], $values["typename"]);
  $form->saveValuesErrorsReset ();
  $route->redirect ("/ipsets", "");
});

Routing with variable parameters

$route
->get ("zone/{zone}/{viewname}/add(\?{fill})?",
function ($zone, $viewname, $fill) use ($route)
{
  // Use the $zone, $viewname, $fill variables from the URL
  // The fill parameter is optional, and may return null
}
');

Debug the routing analysis

Activate the debug by using :

$route = new route ();
$route->debug = 1;

The class definition

Class \route

Namespace \

Description

 The routing module, base of the DomFramework

Properties

public $allowSlashes=true;
 Allow slashes in the url when matching the regex
public $authenticationURL;
 Authentication URL used if a 401 error is raised. If none is defined,
 just display a "Unauthorized" message
public $debug=0;
 The debug mode :
 0:NoDebug, 1:routing, 2:more debug (developpement)
public $errors;
 Provide the the class catch errors in routing.
 Must be provided in an array(class,method);
public $layout=false;
 Filename in views containing the HTML layout. Without .html at the end
public $mapRoute;
 The matching route comparison
public $method="";
 The method used to ask the page
public $module;
 The module name
public $output="html";
 Output type to no previous catched renderer (allow : json, xml, txt html)
public $preroute="";
 Preroute used in modules
public $ratelimiter;
 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 $replacement=array ();
 Array to search/replace
public $title=" ";
 Title by default : space to be compatible with HTML5
public $variable=array ();
 Array to variable definition
public $viewClass=false;
 Filename of class containing the presentation layer
public $viewErrorClass;
 Classname containing the error layer
public $viewErrorMethod;
 Method apply to class object to display the error
public $viewMethod=false;
 Method apply to class object to display the $result

Methods

public function __construct ()
 The route constructor : initialize the parameters

public function baseURL ($module=false, $absolute=false)
 Return the baseURL of the site
 Always finish with a slash
 @param string|null $module The module name (if thereis one)
 @param boolean|null $absolute Return the baseURL in absolute
 @return string The URL base

public function baseURLmodule ()
 Return the baseURL of the module
 Always finish with a slash
 @return string The baseURL for the module

public function baseURLresource ()
 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
 @return string The baseURL for the resource

public function baseURLset ($baseURL)
 Define the base URL of the site
 @param string $baseURL The base URL of the site

public function delete ($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";
       });
 @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 error ($e)
 Print an error page. If a custom page exists, use it
 @param object $e Exception to print
 @return bool true after the error is displayed

public function get ($route, $function)
 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 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 lastValidGetPage ()
 Return the last valid get page to return
 @return The last valid page URL

public function lastValidGetPageRedirect ()
 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 map ($route, $function)
 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 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 mapRoute ()
 Return the mached route search

public function method ()
 Return the HTTP method used to connect to the page
 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 multi ($methods, $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}',
                   function ($name) {
         echo "Hello, $name";
       });
 @param string $methods The allowed methods, separated by commas (,)
 @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 post ($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";
       });
 @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 put ($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";
       });
 @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 redirect ($destURL, $module="", $permanent=false)
 Do all the routing with redirections
 $destURL can be absolute or relative.
 - absolute : "/main" redirect to "baseURL/mail"
 - relative : "main" redirect to "./main"
 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 Permanent redirect (false by default)
 @return Exit of the PHP after doing the redirection

public function requestURL ($absolute=false)
 Return the complete URL used to see this page
 @param boolean|null $absolute Return the absolute URL
 @return string the complete URL used to see this page