Different directory structure #2462
Replies: 4 comments 10 replies
-
Hey @fedemengo-genuino! 👋 Yes! You can move your However, since you are not using the default architecture, you will need to specify where to find your files. For example, you will need to specify the namespace of your controller instead of only typing their name. Route.group(() => {
// Will load `App/User/Controller/UsersController.ts`
Route.get('/', 'UsersController.index')
}).namespace('App/User/Controllers') |
Beta Was this translation helpful? Give feedback.
-
Is there anything special about
And I created a controller
My idea is to use something like
But just by putting the controller I get I also tried adding
In |
Beta Was this translation helpful? Give feedback.
-
In my projects (both v4.1 and v5), I have similar setup with routes, controllers, validators, models, listeners, etc. grouped into modules. The loading of all those stuff can be done inside each module using the Service Provider. Check v4 docs (afaik, docs for this topic in v5 is missing but you can check example from lucid package). With this approach, all you have to do to wire them to the app is declaring the provider in "providers": [
"./providers/AppProvider",
"@adonisjs/core",
"@adonisjs/lucid",
"./modules/MyModule/providers/MyModuleProvider", //👈 Here
], Provider example (v4.1 will be similar): // modules/MyModule/providers/MyModuleProvider.ts
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
export default class MyModuleProvider {
public static needsApplication = true;
constructor(protected _app: ApplicationContract) {}
private async _loadConfig() { /* Load from `config` directory */ }
private async _registerBindings() { /* Register ioc bindings */}
public async register() {
await this._loadConfig();
await this._registerBindings();
}
private async _registerEvents() {
await import('../start/events'); // Your module event subscription
}
private async _registerRoutes() {
await import('../start/routes'); // Your module routes
}
public async _registerRules() {
await import('../start/module-custom-validation-rules');
}
public async boot() {
await this._registerEvents();
await this._registerRules();
await this._registerRoutes();
}
// ...
} You can even make custom commands, extending from the base
|
Beta Was this translation helpful? Give feedback.
-
adonis is great, except with this folder structuring and importing my classes ( controllers, services... import('#controllers/...) is error prone ) |
Beta Was this translation helpful? Give feedback.
-
I was wondering if it's possible to setup an adonis project to use a different directory structure, specifically I'd prefer to have my files in the following way
So basically I'd like to have controllers/validators/middleware/etc grouped by the entity they are managing
Package version
Lucid 6.2.1 with Adonis 4.1.0
Node.js and npm version
v15.11.0 and 7.6.3
Beta Was this translation helpful? Give feedback.
All reactions