const classafBedSheet::Route

sys::Obj
  afBedSheet::Route

Matches HTTP Requests to response objects.

URL matching is case-insensitive and trailing slashes that denote index or directory directory pages are ignored.

Response Objects

A Route may return any response object, be it Text, HttpStatus, File, or any other. It simply returns whatever is passed into the ctor.

Example, this matches the URL /greet and returns the string Hello Mum!

Route(`/greet`, Text.fromPlain("Hello Mum!")) 

And this redirects any request for /home to /greet

Route(`/home`, HttpRedirect.movedTemporarily(`/greet`)) 

You can use glob expressions in your URL, so:

Route(`/greet/*`, ...) 

Response Methods

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 MethodCall object.

Route(`/greet`, MyPage#hello)

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

Examples:

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!

Method Invocation

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.

make

Source

new make(Uri url, Obj response, Str httpMethod := "GET")

Creates a Route that matches on the given URL glob pattern. urlGlob must start with a slash "/". Example:

Route(`/index/**`)

httpMethod may specify multiple HTTP method separated by a space.

Route(`/index/**`, MyClass#myMethod, "GET HEAD")
matchHint

Source

virtual Str matchHint()

A hint at what this route matches on. Used for debugging and in 404 / 500 error pages.

responseHint

Source

virtual Str responseHint()

A hint at what response this route returns. Used for debugging and in 404 / 500 error pages.

toStr

Source

virtual override Str toStr()