Matches HTTP Requests to response objects.
URL matching is case-insensitive and trailing slashes that denote index or directory directory pages are ignored.
Example, this matches the URL
/greet and returns the string
Route(`/greet`, Text.fromPlain("Hello Mum!"))
And this redirects any request for
You can use glob expressions in your URL, so:
Routes may return MethodCall instances that call a Fantom method. To use, pass in the method as the response object. On a successful match, the
Route will convert the method into a
Method matching can also map URL path segments to method parameters and is a 2 stage process:
Stage 1 - URL Matching
Wildcards are used to capture string sections from the request URL to be used as method arguments.
Wildcard syntax is:
/*captures a path segment
/**captures all remaining path segments
URL glob captures ------------ --- ---------- -- ------------- /user/ --> /user/* => default(*) /user/42 --> /user/* => "42" /user/42/ --> /user/* => "42" /user/42/dee --> /user/* => no match /user/ --> /user/** => default(*) /user/42 --> /user/** => "42" /user/42/ --> /user/** => "42" /user/42/dee --> /user/** => "42/dee"
(*) If the corresponding method argument has a default value, it is taken, otherwise no match.
Assuming you you have an entity object, such as
User, with an ID field; you can contribute a
ValueEncoder that inflates (or otherwise reads from a database)
User objects from a string version of the ID. Then your methods can declare
User as a parameter and BedSheet will convert the captured strings to User objects for you!
Handler methods may be non-static. They they belong to an IoC service then the service is obtained from the IoC registry. Otherwise the containing class is autobuilt. If the class is
const, the instance is cached for future use.
Creates a Route that matches on the given URL glob pattern.
urlGlobmust start with a slash "/". Example:
httpMethodmay specify multiple HTTP method separated by a space.
Route(`/index/**`, MyClass#myMethod, "GET HEAD")
virtual Str matchHint()
A hint at what this route matches on. Used for debugging and in 404 / 500 error pages.
virtual Str responseHint()
A hint at what response this route returns. Used for debugging and in 404 / 500 error pages.
virtual override Str toStr()