Documentation
    Preparing search index...

    Router class exposes a unified API to register new routes, group them or create route resources.

    const router = new Router()

    router.get('/', async function () {
    // handle request
    })
    Index

    Constructors

    • Creates a new Router instance

      Parameters

      • app: Application<any>

        The AdonisJS application instance

      • encryption: Encryption

        Encryption service for signed URLs

      • qsParser: Qs

        Query string parser for URL generation

      Returns Router

    Properties

    usingDomains: boolean = false

    A flag to know if routes for explicit domains have been registered. The boolean is computed after calling the "commit" method.

    matchers: RouteMatchers = ...

    Shortcut methods for commonly used route matchers

    qs: Qs

    Query string parser for making URLs

    urlBuilder: {
        urlFor: UrlFor<never>;
        signedUrlFor: UrlFor<never, SignedURLOptions>;
    }

    The URLBuilder offers a type-safe API for creating URL for pre-registered routes or the route patterns.

    We recommend using the URLBuilder over the "makeUrl" and "makeSignedUrl" methods.

    Accessors

    • get commited(): boolean

      Check if routes have been committed to the store. Once routes are committed, defining new set of routes will have no impact

      Returns boolean

    Methods

    • Define an array of middleware to use on all the routes. Calling this method multiple times pushes to the existing list of middleware

      Parameters

      • middleware: LazyImport<MiddlewareAsClass>[]

        Array of middleware classes to apply globally

      Returns this

      Current Router instance for method chaining

    • Define a collection of named middleware. The defined collection is not registered anywhere, but instead converted in a new collection of functions you can apply on the routes, or router groups.

      Type Parameters

      Parameters

      • collection: NamedMiddleware

        Object mapping middleware names to middleware classes

      Returns {
          [K in string | number | symbol]: <
              Args extends
                  | []
                  | [
                      Parameters<
                          InstanceType<UnWrapLazyImport<NamedMiddleware[K]>>["handle"],
                      >[2],
                  ]
                  | [
                      Parameters<InstanceType<UnWrapLazyImport<(...)[(...)]>>["handle"]>[2]?,
                  ],
          >(
              ...args: Args,
          ) => {
              name: K;
              reference: MiddlewareAsClass | LazyImport<MiddlewareAsClass>;
              args: Args[0];
              handle: (
                  resolver: ContainerResolver<any>,
                  ...args: [ctx: HttpContext, next: NextFn, params?: any],
              ) => any;
          }
      }

      Named middleware functions

    • Add route for a given pattern and methods

      Type Parameters

      • T extends Constructor<any>

      Parameters

      • pattern: string

        The route pattern

      • methods: string[]

        Array of HTTP methods

      • handler: string | RouteFn | [T | LazyImport<T>, GetControllerHandlers<T>?]

        Route handler (function, string, or controller tuple)

      Returns Route<T>

      The created route instance

    • Define a route that handles all common HTTP methods

      Type Parameters

      • T extends Constructor<any>

      Parameters

      • pattern: string

        The route pattern

      • handler: string | RouteFn | [T | LazyImport<T>, GetControllerHandlers<T>?]

        Route handler (function, string, or controller tuple)

      Returns Route<T>

      The created route instance

    post

    • Creates a group of routes. A route group can apply transforms to routes in bulk

      Parameters

      • callback: () => void

        Function that defines routes within the group

      Returns RouteGroup

      The created route group instance

    • Registers a route resource with conventional set of routes

      Parameters

      • resource: string

        The resource name

      • controller: string | Constructor<any> | LazyImport<Constructor<any>>

        Controller to handle the resource

      Returns RouteResource<ResourceActionNames>

      The created route resource instance

    • Register a route resource with shallow nested routes.

      Parameters

      • resource: string

        The resource name

      • controller: string | Constructor<any> | LazyImport<Constructor<any>>

        Controller to handle the resource

      Returns RouteResource<ResourceActionNames>

      The created route resource instance

    • Returns a brisk route instance for a given URL pattern

      Parameters

      • pattern: string

        The route pattern

      Returns BriskRoute

      The created brisk route instance

    • Define matcher for a given param. The global params are applied on all the routes (unless overridden at the route level).

      Parameters

      • param: string

        The parameter name to match

      • matcher: string | RegExp | RouteMatcher

        The matcher pattern (RegExp, string, or RouteMatcher)

      Returns this

      Current Router instance for method chaining

    • Commit routes to the store. The router is freezed after the commit method is called.

      Returns void

    • Finds a route by its identifier. The identifier can be the route name, controller.method name or the route pattern itself.

      When "disableLegacyLookup" is set, the lookup will be performed only using the route name

      Parameters

      • routeIdentifier: string

        Route name, pattern, or controller reference

      • Optionaldomain: string

        Optional domain to search within

      • Optionalmethod: string

        Optional HTTP method to filter by

      • OptionaldisableLegacyLookup: boolean

        Whether to disable legacy lookup strategies

      Returns null | RouteJSON

      Found route or null if not found

    • Finds a route by its identifier. The identifier can be the route name, controller.method name or the route pattern itself.

      An error is raised when unable to find the route.

      When "disableLegacyLookup" is set, the lookup will be performed only using the route name

      Parameters

      • routeIdentifier: string

        Route name, pattern, or controller reference

      • Optionaldomain: string

        Optional domain to search within

      • Optionalmethod: string

        Optional HTTP method to filter by

      • OptionaldisableLegacyLookup: boolean

        Whether to disable legacy lookup strategies

      Returns RouteJSON

      Found route

      Error when route is not found

    • Check if a route exists. The identifier can be the route name, controller.method name or the route pattern itself.

      When "followLookupStrategy" is enabled, the lookup will be performed on the basis of the lookup strategy enabled via the "lookupStrategies" method. The default lookupStrategy is "name" and "pattern".

      Parameters

      • routeIdentifier: string

        Route name, pattern, or controller reference

      • Optionaldomain: string

        Optional domain to search within

      • Optionalmethod: string

        Optional HTTP method to filter by

      • OptionalfollowLookupStrategy: boolean

        Whether to follow the configured lookup strategy

      Returns boolean

      True if route exists, false otherwise

    • Returns a list of routes grouped by their domain names

      Returns { [domain: string]: RouteJSON[] }

      Object mapping domain names to route arrays

    • Generates types for the URL builder. These types must be written inside a file for the URL builder to pick them up.

      Parameters

      • indentation: number = 0

        Indentation level for generated types

      Returns string

      Generated TypeScript types as string

    • Find route for a given URL, method and optionally domain

      Parameters

      • uri: string

        The URI to match

      • method: string

        HTTP method

      • shouldDecodeParam: boolean

        Whether to decode parameters

      • Optionalhostname: null | string

        Optional hostname for domain matching

      Returns null | MatchedRoute

      Matched route or null if no match found

    • Create URL builder instance for a given domain.

      Parameters

      • domain: string

      Returns UrlBuilder

      Instead use "@adonisjs/core/services/url_builder"

    • Make URL to a pre-registered route

      Parameters

      • routeIdentifier: string
      • Optionalparams: any[] | Record<string, any>
      • Optionaloptions: MakeUrlOptions

      Returns string

      Instead use "@adonisjs/core/services/url_builder"

    • Makes a signed URL to a pre-registered route.

      Parameters

      • routeIdentifier: string
      • Optionalparams: any[] | Record<string, any>
      • Optionaloptions: MakeSignedUrlOptions

      Returns string

      Instead use "@adonisjs/core/services/url_builder"