[v4] Add custom routes to Route.resource


Hi! What is the best way to add routes to the Route.resource or create my own RestResource. I can see that the routes are added calling the function _addBasicRoutes, so I want to modify it like the following example:

class RouteResource extends Macroable {
  _addBasicRoutes () {
    this._addRoute('index', this._resourceUrl)
    this._addRoute('store', this._resourceUrl, ['POST'])
    this._addRoute('show', `${this._resourceUrl}/:id`)
    this._addRoute('next', `${this._resourceUrl}/:id/next`) // Add this
    this._addRoute('previous', `${this._resourceUrl}/:id/previous`) // Add this
    this._addRoute('update', `${this._resourceUrl}/:id`, ['PUT'])
    this._addRoute('updateMultiple', `${this._resourceUrl}`, ['PATCH']) // Add this
    this._addRoute('destroy', `${this._resourceUrl}/:id`, ['DELETE'])
    this._addRoute('destroyMultiple', `${this._resourceUrl}`, ['DELETE']) // Add this

Thanks in advice for your help!


How about make this

Route.get(‘user/delete/:id’, ‘UserController.delete’)
Route.get(‘user/create/:id’, ‘UserController.create’)

equal to something like this?

Route.get(‘user’, ‘UserController’, {
‘delete/:id’: ‘delete’,
‘create/:id’: ‘create’


The problem with this aproach is that I will have to make this to all the resources in my api, instead with the resource I only have to do the following:

      .myCustomResource('users', 'UserController')  // <-- this will generate all my routes (Convention over configuration)

Instead of:

    Route.get('user', 'UserController.index')
    Route.get('user/:id', 'UserController.read')
    Route.get('user/:id/next', 'UserController.next')
    Route.get('user/:id/prev', 'UserController.prev')
    Route.post('user', 'UserController.store')
    Route.put('user/:id', 'UserController.update')
    Route.patch('user/:id', 'UserController.update')
    Route.delete('user/:id', 'UserController.store')
    Route.post('user', 'UserController.updateMassive')
    Route.post('user', 'UserController.deleteMassive')

And if I have multiple resources the router file will be huge, so I woukd like to extend or register a new RestResource in AdonisJS with my custom routes.


It was a wonderful idea. Have you tried solving this.


Here are docs on how to extend routes. https://adonisjs.com/docs/4.1/routing#_extending_routes

Feel free to ask questions, if you have stuck somewhere


Thanks virk.

I need some more documentations.

  1. I want to create a custom adonis command like “adonis make:controller”, to create a set of restful services. Can you you provide?

  2. How to create a custom “auth” middleware, apart from the existing jwt, session and basic… Something like token based, which i will store in redis. I currently solved this by using validators.


Hey @sujai!

Let me answer your question.

  1. You can create your own commad by running adonis make:command. If you need to take a look at the current generator for the make:controller command you can find it here.
  2. You can create your own middleware by using adonis make:middleware <name>. Then follow the instruction displayed to add it as named middleware.


Thanks @romain.lanz for the command example. I shall check into it further.
But regarding my second doubt. i need to create a new “Auth” schemes not the middleware.

The current available “auth” schemes are session(email and password), basic (username and password), jwt. All the three uses “model” as “App/Models/User” and and serializer as “lucid”.

I want to create a new auth scheme with cusom serializer and custom model.(redis and token based auth for api requests).

Sujai K