Routes::route PHP Méthode

route() public static méthode

public static route ( $uri )
    public static function route($uri)
    {
        $qs = '';
        if (static::$allow_query && strpos($uri, '?') !== false) {
            // Break the query string off and attach later
            $qs = '?' . parse_url($uri, PHP_URL_QUERY);
            $uri = str_replace($qs, '', $uri);
        }
        // Is there a literal match?
        if (isset(static::$routes[$uri])) {
            return static::$routes[$uri] . $qs;
        }
        // Loop through the route array looking for wild-cards
        foreach (static::$routes as $key => $val) {
            // Convert wild-cards to RegEx
            $key = str_replace(':any', '.+', $key);
            $key = str_replace(':num', '[0-9]+', $key);
            $key = str_replace(':nonum', '[^0-9]+', $key);
            $key = str_replace(':alpha', '[A-Za-z]+', $key);
            $key = str_replace(':alnum', '[A-Za-z0-9]+', $key);
            $key = str_replace(':hex', '[A-Fa-f0-9]+', $key);
            // Does the RegEx match?
            if (preg_match('#^' . $key . '$#', $uri)) {
                // Do we have a back-reference?
                if (strpos($val, '$') !== false && strpos($key, '(') !== false) {
                    $val = preg_replace('#^' . $key . '$#', $val, $uri);
                }
                return $val . $qs;
            }
        }
        return $uri . $qs;
    }

Usage Example

Exemple #1
0
<?php

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_regex_encoding('UTF-8');
spl_autoload_register(function ($class) {
    $class = strtolower($class);
    require_once "class.{$class}.php";
});
$routes = ["" => "home", "index.php" => "home", "clock" => "clock", "clockIn" => "clockin", "clockOut" => "clockout"];
$req = preg_replace('/^\\/timeclock\\/([^\\/]*)\\/?$/', "\$1", $_SERVER["REQUEST_URI"]);
Routes::register($routes);
Routes::route($req);