✨ Basics
Controllers

The controllers

Controllers in AdonisJS

The controllers will handle the business logic of your application. They are the heart of your application. They will be responsible for handling the requests and responses of your application.

Creating a controller

There are two ways to create a controller in AdonisJS.

1. Using the command line

You can create a controller using the command line.

node ace make:controller user_controller

It will create a new file in the app/controllers folder named user_controller.ts.

2. Create a controller manually

You can create a controller manually by creating a new file in the app/controllers folder.

app/controllers/user_controller.ts
export default class AccountController {
}

Controller methods

A controller can have multiple methods. Each method is a function that is defined inside the controller.

app/account/controllers/account_controller.ts
import { HttpContext } from "@adonisjs/core/http";
 
  async update({ request, response, auth, session, logger }: HttpContext) {
    try {
      const payload = await updateUserValidator.validate(request.all());
      const user = auth.user!;
      await this.accountService.updateUser(user, payload);
      session.flash("toast", {
        type: "success",
        message: "Your account has been successfully updated"
      });
      return response.redirect().toRoute("dashboard.account");
    } catch (error) {
      logger.fatal(error);
      session.flash("errors", "An unexpected error occurred, please try again later or contact the support.");
    }
  }

Injecting dependencies into controllers

app/account/controllers/account_controller.ts
import { updateUserValidator } from "../validators/account_validator.js";
import { HttpContext } from "@adonisjs/core/http";
import { inject } from "@adonisjs/core";
import AccountService from "../services/account_service.js";
 
 
@inject()
export default class AccountController {
 
  private accountService: AccountService;
 
 
  constructor(accountService: AccountService) {
    this.accountService = accountService;
  }
 
  async update({ request, response, auth, session, logger }: HttpContext) {
    try {
      const payload = await updateUserValidator.validate(request.all());
      const user = auth.user!;
      await this.accountService.updateUser(user, payload);
      session.flash("toast", {
        type: "success",
        message: "Your account has been successfully updated"
      });
      return response.redirect().toRoute("dashboard.account");
    } catch (error) {
      logger.fatal(error);
      session.flash("errors", "An unexpected error occurred, please try again later or contact the support.");
    }
  }
}

As you can see, we are injecting the AccountService into the controller. This allows us to use the service methods within the controller, with the inject decorator.

See more

Tips! You can generate routes & controllers with already everything imported with the node ace make:module module_name command.