Skip to main content
The 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

import router from '#services/router'
import { Router } from '@adonisjs/core/http'

Properties

routes
Route[]
Collection of all registered routes
precompiler
RoutePrecompiler
Precompiler instance for optimizing routes

HTTP Methods

get()

Register a GET route.
router.get('/users', [UsersController, 'index'])
pattern
string
URL pattern for the route
handler
RouteHandler
Route handler function or controller reference
Returns: Route

post()

Register a POST route.
router.post('/users', [UsersController, 'store'])
pattern
string
URL pattern for the route
handler
RouteHandler
Route handler function or controller reference
Returns: Route

put()

Register a PUT route.
router.put('/users/:id', [UsersController, 'update'])
pattern
string
URL pattern for the route
handler
RouteHandler
Route handler function or controller reference
Returns: Route

patch()

Register a PATCH route.
router.patch('/users/:id', [UsersController, 'update'])
pattern
string
URL pattern for the route
handler
RouteHandler
Route handler function or controller reference
Returns: Route

delete()

Register a DELETE route.
router.delete('/users/:id', [UsersController, 'destroy'])
pattern
string
URL pattern for the route
handler
RouteHandler
Route handler function or controller reference
Returns: Route

any()

Register a route that responds to all HTTP methods.
router.any('/webhook', [WebhookController, 'handle'])
pattern
string
URL pattern for the route
handler
RouteHandler
Route handler function or controller reference
Returns: Route

Route Groups

group()

Group multiple routes together to apply shared configuration.
router.group(() => {
  router.get('/posts', [PostsController, 'index'])
  router.post('/posts', [PostsController, 'store'])
}).prefix('/api/v1').middleware('auth')
callback
() => void
Callback function containing route definitions
Returns: RouteGroup

prefix()

Add a prefix to all routes in a group.
router.group(() => {
  // Routes here
}).prefix('/api/v1')
prefix
string
URL prefix to prepend to all routes
Returns: RouteGroup

middleware()

Apply middleware to all routes in a group.
router.group(() => {
  // Protected routes
}).middleware(['auth', 'verified'])
middleware
string | string[]
Middleware name(s) to apply
Returns: RouteGroup

Resource Routes

resource()

Register RESTful resource routes.
router.resource('posts', PostsController)
This creates the following routes:
  • GET /posts - index
  • GET /posts/create - create
  • POST /posts - store
  • GET /posts/:id - show
  • GET /posts/:id/edit - edit
  • PUT/PATCH /posts/:id - update
  • DELETE /posts/:id - destroy
resource
string
Resource name
controller
Constructor
Controller class handling the resource
Returns: ResourceRoute

apiOnly()

Create API-only resource routes (excludes create and edit).
router.resource('posts', PostsController).apiOnly()
Returns: ResourceRoute

only()

Include only specific resource routes.
router.resource('posts', PostsController).only(['index', 'show'])
actions
string[]
Array of action names to include
Returns: ResourceRoute

except()

Exclude specific resource routes.
router.resource('posts', PostsController).except(['destroy'])
actions
string[]
Array of action names to exclude
Returns: ResourceRoute

Route Configuration

as()

Assign a name to a route for URL generation.
router.get('/dashboard', [DashboardController, 'index'])
  .as('dashboard')
name
string
Route name
Returns: Route

where()

Add parameter constraints using regular expressions.
router.get('/posts/:id', [PostsController, 'show'])
  .where('id', /^[0-9]+$/)
param
string
Parameter name
matcher
RegExp | string
Regular expression or string pattern
Returns: Route

domain()

Restrict a route to a specific domain.
router.get('/', [HomeController, 'index'])
  .domain('blog.example.com')
domain
string
Domain name or pattern
Returns: Route

Route Matchers

matchers()

Define global parameter matchers.
router.matchers({
  id: /^[0-9]+$/,
  slug: /^[a-z0-9-]+$/
})
matchers
Record<string, RegExp>
Object mapping parameter names to regular expressions
Returns: this

Utilities

commit()

Finalize and commit all registered routes.
router.commit()
Returns: void

toJSON()

Get a JSON representation of all routes.
const routes = router.toJSON()
Returns: RouteJSON[]

Example Usage

import router from '#services/router'
import UsersController from '#controllers/users_controller'
import PostsController from '#controllers/posts_controller'

// Basic routes
router.get('/', async ({ view }) => {
  return view.render('home')
})

// Controller routes
router.get('/users', [UsersController, 'index'])
router.post('/users', [UsersController, 'store'])

// Named routes
router.get('/dashboard', [DashboardController, 'index'])
  .as('dashboard')

// Routes with constraints
router.get('/posts/:id', [PostsController, 'show'])
  .where('id', /^[0-9]+$/)

// Route groups
router.group(() => {
  router.resource('posts', PostsController).apiOnly()
  router.resource('comments', CommentsController)
}).prefix('/api').middleware('auth')

// Subdomain routes
router.group(() => {
  router.get('/', [BlogController, 'index'])
}).domain(':tenant.example.com')

Type Safety

The router provides full TypeScript support with type-safe route definitions:
import type { HttpRouterService } from '@adonisjs/core/types'

const router: HttpRouterService = await app.container.make('router')