I'm trying out AdonisJS, but I'm stuck trying to get a service injected into a controller. No matter what I try, the hs
constructor argument in VersionsController
remains undefined
.
I've also experimented with annotating the VersionController
constructor with @inject(['@ioc:Service/HashService'])
, but with no luck. I'm not sure though if @inject
is the right way for Adonis.js v5.
How do I properly inject a service class into a controller?
/providers/AppProvider.ts
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import HashService from 'App/Services/HashService';
export default class AppProvider {
protected app: ApplicationContract;
constructor(app: ApplicationContract) {
this.app = app;
}
public register() {
this.app.container.singleton('@ioc:Service/HashService', () => {
return new HashService();
});
}
public async boot() {
// IoC container is ready
}
public async ready() {
// App is ready
}
public async shutdown() {
// Cleanup, since app is going down
}
}
/app/Services/HashService.ts
'use strict'
export default class HashService {
async test() {}
}
module.exports = HashService;
app/Controllers/Http/VersionsController.ts
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import { HashService } from '@ioc:Service/HashService'
export default class VersionsController {
protected hs: HashService
constructor(hs: HashService) {
this.hs = hs;
console.log("hashservice is " + hs);
}
public async get(ctx: HttpContextContract) {
return [
{
id: 1,
title: 'a'
}
]
}
public async put(ctx: HttpContextContract) {
return "PUT";
}
}
.adonisrc.json
{
"typescript": true,
"commands": [
"./commands",
"@adonisjs/core/build/commands/index.js",
"@adonisjs/repl/build/commands"
],
"exceptionHandlerNamespace": "App/Exceptions/Handler",
"aliases": {
"App": "app",
"Config": "config",
"Database": "database",
"Contracts": "contracts"
},
"preloads": [
"./start/routes",
"./start/kernel"
],
"providers": [
"./providers/AppProvider",
"@adonisjs/core"
],
"aceProviders": [
"@adonisjs/repl"
]
}