Router class provides a fluent API for defining HTTP routes in your AdonisJS application. It supports various HTTP methods, route groups, resource routes, and middleware.
Importing
Properties
Collection of all registered routes
Precompiler instance for optimizing routes
HTTP Methods
get()
Register a GET route.URL pattern for the route
Route handler function or controller reference
Route
post()
Register a POST route.URL pattern for the route
Route handler function or controller reference
Route
put()
Register a PUT route.URL pattern for the route
Route handler function or controller reference
Route
patch()
Register a PATCH route.URL pattern for the route
Route handler function or controller reference
Route
delete()
Register a DELETE route.URL pattern for the route
Route handler function or controller reference
Route
any()
Register a route that responds to all HTTP methods.URL pattern for the route
Route handler function or controller reference
Route
Route Groups
group()
Group multiple routes together to apply shared configuration.Callback function containing route definitions
RouteGroup
prefix()
Add a prefix to all routes in a group.URL prefix to prepend to all routes
RouteGroup
middleware()
Apply middleware to all routes in a group.Middleware name(s) to apply
RouteGroup
Resource Routes
resource()
Register RESTful resource routes.GET /posts- indexGET /posts/create- createPOST /posts- storeGET /posts/:id- showGET /posts/:id/edit- editPUT/PATCH /posts/:id- updateDELETE /posts/:id- destroy
Resource name
Controller class handling the resource
ResourceRoute
apiOnly()
Create API-only resource routes (excludes create and edit).ResourceRoute
only()
Include only specific resource routes.Array of action names to include
ResourceRoute
except()
Exclude specific resource routes.Array of action names to exclude
ResourceRoute
Route Configuration
as()
Assign a name to a route for URL generation.Route name
Route
where()
Add parameter constraints using regular expressions.Parameter name
Regular expression or string pattern
Route
domain()
Restrict a route to a specific domain.Domain name or pattern
Route
Route Matchers
matchers()
Define global parameter matchers.Object mapping parameter names to regular expressions
this
Utilities
commit()
Finalize and commit all registered routes.void
toJSON()
Get a JSON representation of all routes.RouteJSON[]