File src/class/Http/Router.php

class Lia\Http\Router

HTTP router.
You configure it, add routes to it, then get routes from it, for a given request url & request method.

Constants

Properties

  • public string $varDelim = '/'; A string of characters that can be used as delimiters for dynamic url portions.
    Affects all routes on this router.
    This string will be passed through preg_quote($varDelim, '/') before being used in a regex pattern.
  • public string $theme_name = 'raw'; raw, html_page, or a theme_name for your default theme.
  • public array $routeMap = [];
  • public array $hidden_extensions = ['php']; Array of extensions to hide when generating routes from files. Defaults to php only.

array<int index, string extension> - extension should be lowercase text only (no period!)

  • public array $index_names = ['index.php']; Array of index file names to hide when generating routes from files. Defaults to 'index.php' only.
    array<int index, string index_file_name>
  • public bool $add_trailing_slash = true; true to add a trailing slash to route patterns that do NOT have an extension. false to disable.
  • public array $route_args = []; array<string arg_name, mixed value> Arguments to pass to every route. Each arg will be a defined $arg_name
  • public bool $allowExecutableFile = false; Set true to allow executable files to handle routes.

Methods

  • public function clean_url(string $dirty_url): string Standardize URL Path, replacing:

  • space with '+'

  • quote with '%27'

  • multiple slashes with a single slash

  • public function has_static_route(string $path, string $method="GET"):bool Check if static route exists. Does not check dynamic routes.

  • public function addDirectoryRoutes(string $dir, string $base_url, array $with_args=[]): void Add a route to each file in $dir. Handles static and php files.

NOTICE: This Http Router returns a list of routes & does not control how the calling code includes the file, or how it passes the $with_args.

  • public function addRoute(string $pattern, mixed $target, array $methods=["GET"],array $args []): void Add a route.

NOTICE: This Http Router returns a list of routes & does not control how the calling code calls the target, or how it passes the $args.

  • public function getRoutes(string $url, string $http_method = "GET"): array Get array of routes for the request.

  • public function patterns_from_dir(string $dir, string $prefix=''): array Get an array of route patterns from files in a directory.

  • public function pattern_from_file(string $relFile): string Get a route pattern from a relative file path.

'php' extension is removed. Other extensions are unchanged.

  • protected function separate_optional_from_decoded_pattern(array $original_parsed): array Facilitates optional paramaters

Processes a parsed pattern into an array of valid parsed patterns where the original pattern may contain details for optional paramaters

  • protected function decode_pattern(string $pattern): array Convert a pattern into a decoded array of information about that pattern

Examples:

  • /blog/{category}/{post}/ is valid for urls like /blog/animals/cute-cats/
  • /blog/{category}+{post}/ has no dynamic paramaters because the default delimiter is a /.
  • /blog/{category}/@GET.{post}/ is valid for GET /blog/kindness/is-awesome/ but not for POST request
  • /@POST.dir/sub/@GET.file/ is valid for both POST /dir/sub/file/ and GET /dir/sub/file/

Methods: @POST, @GET, @PUT, @DELETE, @OPTIONS, @TRACE, @HEAD, @CONNECT

  • We do not currently check the name of the method, just @ABCDEF for length 3-7
  • These must appear after a / or after another '@METHOD.' or they will be taken literally as characters in the pattern.
  • lower case is not valid
  • Each method MUST be followed by a period (.)

Paramaters:

  • Set varDelim on the router to change how named paramaters are separated.
  • Default varDelim is /. It is a string list of characters.
  • {Param_Names} can include a-z, A-Z, and underscores (_).
  • {param} MAY be at the end of a pattern with no trailing delimiter, like /blog/{slug}
  • {?optional_paramaters} are supported, but shouldn't be used. They're confusing to use & may be removed in a future update.

TODO

  • TODO {paramName:regex} to specify a named paramater that must match a specific regex.

  • protected function extract_url_paramaters(array $decoded_pattern, string $url): array Get an array of named paramaters from a url.

  • protected function url_to_regex(string $url): string Convert an actual url into regex to match against testPatterns.