Routing

The routing feature of Liten, lets you map URI’s to HTTP request methods (GET, POST, PUT, DELETE, OPTIONS).

When a route matches, the attached route handling function will be executed. The route handling function must be a callable. Only the first route matched will be handled. When no matching route is found, an ‘HTTP/1.1 404 Not Found’ status code will be returned.

Shorthands for single request methods are provided:

There is also an alternative shorthand for request methods in the following format:

Here are some shorthand examples to above:

Another alternative to above is route matching. A particular code will run based on the HTTP Method sent.

Here is an example of using GET and POST.

Note: Routes must be hooked before $app->run(); is being called.

Route Patterns

Route patterns can be static or dynamic.

  • Static Route Patterns are basic URIs, e.g. /about.
  • Dynamic Route Patterns are Perl-compatible regular expressions (PCRE) that resemble URIs, e.g. /api/(\d+)

Commonly used subpatterns within Dynamic Route Patterns are:

  • \d+ = One or more digits (0-9)
  • \w+ = One or more word characters (a-z 0-9 _)
  • [a-z0-9_-]+ = One or more word characters (a-z 0-9 _) and the dash (-)
  • .* = Any character (including /), zero or more
  • [^/]+ = Any character but /, one or more

Downloading the PHP PCRE Cheatsheet may come in handy.

Subpatterns must be parenthesized in order to work. See example below:

When multiple subpatterns are defined, the route handling parameters are interpreted in the order which they are defined.

Subrouting / Grouping Routes

Use $app->group($baseroute, $fn) to group a collection of routes onto a subroute pattern. The subroute pattern is prefixed onto all following routes defined in the scope. e.g. Mounting a callback $fn onto /posts will prefix /posts onto all following routes.

Custom Error

Last Modified: Oct 4, 2016 @ 2:29 PM

Liten Framework Forums Routing

This topic contains 0 replies, has 1 voice, and was last updated by  parkerj 3 years, 7 months ago.

You must be logged in to reply to this topic.