Router class final

Fluent router (BLUEPRINT_V2 §7.3.2). Routes are registered during boot and compiled once into per-method RadixTrees.

Constructors

Router()

Properties

baseUrl ↔ String
Base URL used for absolute URLs (set from APP_URL at boot).
getter/setter pair
fallbackRoute → RouteEntry?
The fallback route, if any.
no setter
hasBindings → bool
Whether any route model binding is registered.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isCompiled → bool
Whether compile has run.
no setter
patterns → Map<String, RegExp>
Router-wide parameter patterns (Route::pattern).
no setter
routes → List<RouteEntry>
All registered routes in registration order.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
signer ↔ UrlSigner?
Signer used by signedUrl (set from APP_KEY at boot).
getter/setter pair

Methods

allowedMethods(String path, {String? host}) → List<String>
Methods that have a route for path (used for 405 / OPTIONS).
any(String path, Handler handler) → RouteEntry
Registers handler for every standard method.
apiResource(String name, ResourceController controller, {List<String>? only, List<String>? except, Map<String, String> parameters = const <String, String>{}, List<Object> middleware = const <Object>[]}) → void
Registers index, store, show, update (PUT+PATCH) and destroy for name (posts → /posts, /posts/{post}), named posts.index etc. Nested names (posts.comments) produce nested paths.
bind(String param, RouteBinder resolver, {bool orFail = true}) → void
Resolves every route parameter named param through resolver after the route's RunsBeforeBindings middleware (authentication, rate limits) and before the rest; the result is available via request.binding<T>(). With orFail, a null result renders a 404.
bindModel<T extends Model>(String param, ModelDefinition<T> definition) → void
Route model binding: {post} becomes a Post, read with request.binding<Post>('post'), or a 404. Explicit, because a Dart handler has no parameter types to tell a model from an id.
compile(MiddlewareRegistry registry) → void
Builds the lookup trees and resolves middleware aliases. Validates that route names are unique. Optional parameters are expanded into one tree entry per prefix (/posts/{id?} → /posts and /posts/{id}).
delete(String path, Handler handler) → RouteEntry
A DELETE route.
fallback(Handler handler) → RouteEntry
Registers the route used when nothing else matches a GET request.
get(String path, Handler handler) → RouteEntry
A GET (and HEAD) route: router.get('/users/{id}', handler).
group(void routes(Router router), {String prefix = '', String name = '', List<Object> middleware = const <Object>[], Map<String, Object> where = const <String, Object>{}, bool scopeBindings = false, String? domain}) → void
Registers routes with stacked prefix, name prefix, middleware and parameter constraints (where).
has(String name) → bool
Whether a route named name exists.
hasValidSignature(Request request) → bool
Whether request carries a valid signature produced by signedUrl.
lookup(String method, String path, {String? host}) → RouteMatch<RouteEntry>?
Looks up the route for method + path, honouring constraints. HEAD falls back to GET. The fallback route (if any) answers GET requests that match nothing else.
match(List<String> methods, String path, Handler handler) → RouteEntry
Registers handler for the given methods.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
options(String path, Handler handler) → RouteEntry
An OPTIONS route.
patch(String path, Handler handler) → RouteEntry
A PATCH route.
pattern(String param, Object regex) → void
Router-wide constraint for every parameter named param.
permanentRedirect(String from, String to) → RouteEntry
Permanent (301) redirect.
post(String path, Handler handler) → RouteEntry
A POST route.
put(String path, Handler handler) → RouteEntry
A PUT route.
redirect(String from, String to, {int status = 302}) → RouteEntry
Redirects from to to (302 by default).
resolveBindings(Request request) → Future<Handler?>
Runs the binders for the matched route of request, in path order. Answers the route's missing handler when it should reply instead.
resource(String name, WebResourceController controller, {List<String>? only, List<String>? except, Map<String, String> parameters = const <String, String>{}, List<Object> middleware = const <Object>[]}) → void
Like apiResource plus create and edit for HTML forms.
signedUrl(String name, {Map<String, Object?> params = const <String, Object?>{}, Map<String, Object?> query = const <String, Object?>{}, Duration? expires, bool absolute = true}) → String
Generates a signed URL (absolute by default) that ValidateSignatureMiddleware can verify; expires adds expiry.
toString() → String
A string representation of this object.
inherited
url(String name, {Map<String, Object?> params = const <String, Object?>{}, Map<String, Object?> query = const <String, Object?>{}, bool absolute = false}) → String
Generates a path for the named route, substituting params and appending query. Optional parameters may be omitted; with absolute the baseUrl is prepended.
ws(String path, WebSocketHandler handler, {bool compress = true, int maxPayloadBytes = Response.defaultWebSocketMaxPayloadBytes, int maxBufferedBytes = Response.defaultWebSocketMaxBufferedBytes, int slowClientCode = 1008}) → RouteEntry
A WebSocket endpoint: the route's middleware (auth, throttle, ...) runs first, then the connection is upgraded and handler gets the socket and the request (route parameters, Auth.user()) in the request's scope. A plain request gets 426 Upgrade Required. A WebSocket route; compress false turns permessage-deflate off, and a client with more than maxBufferedBytes waiting to be sent is closed with slowClientCode (see Response.webSocket).

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

expandOptional(String path) → List<String>
Expands trailing optional parameters into every path variant.
joinPaths(String base, String path) → String
Joins two path fragments into a normalized path (/ for root).