How do I autoload a different folder

Adonis’ package.json contains an object entry called “autoload” and within it the app folder has been autoloaded so as to exist in the App namespace.

I want to load another folder called modules where I can place different modules that all exists as self sustaining little applications.

"autoload": {
    "App": "./app",
    "Modules": "./modules"
}

In modules I have a routes file and a controllers folder all nested in a module e.g orders. I want to be able to define routes within the orders module like this

Route.get('/order', 'Modules/order/app/Controllers/Http/OrderController.index')

However, even with autoload, adonis still looks for a modules folder inside app/Http.

How do I use autoload to determine a different folder in which I can define controllers, models etc.

1 Like

Maybe a little too late but here goes…

I tried the autoload via the package.json without much success.

My current hack is via the register() method on a provider.

Say you have a folder called myapps at the root of your application. You could load the Orders app via a provider.

const OrderManager = require('../myapps/Orders')

this.app.singleton('Modules/Order', (app) => {
      return new OrderManager()
    })

You can add a routes file by doing the following;

  1. Add a dependency to the route manager const Route = require('@adonisjs/framework/src/Route/Manager')

  2. Create your routes as per usual


I realized I have to wrestle with way too many things at any given time just to make things work. This defeats the whole purpose of using a framework.

So I am just using the workflows that adonis supports well enough at the moment :nerd_face:

1 Like