Skip to content

Controller

Controllers are responsible for handling HTTP requests, interacting with Models, and returning appropriate responses. In Laratype, controllers are typically used to organize request handling logic and responses, making the codebase clearer and easier to maintain.

INFO

Controllers are organized in the src/http/controllers directory.

Creating a Controller

Generate Controller

You can use the Sauf command to create a new controller:

sh
$ npx sauf make:controller RegisterController
sh
$ pnpx sauf make:controller RegisterController
sh
$ bunx sauf make:controller RegisterController

Laratype will create a new controller file in the src/http/controllers/ directory named RegisterController.ts.

TIP

You can create multiple controllers at once by passing multiple names.

See more
sh
$ npx sauf make:controller RegisterController UserController
sh
$ pnpx sauf make:controller RegisterController UserController
sh
$ bunx sauf make:controller RegisterController UserController

Writing Controller

Controllers in Laratype extend the Controller class available in the @laratype/http package. You can define methods in the controller to handle specific actions, such as creating, reading, updating, or deleting resources.

ts
import { Controller } from "@laratype/http";
import CreateUserRequest from "../requests/CreateUserRequest";
import { User } from "../../models/User";
export default class RegisterController extends Controller {
  async register(request: CreateUserRequest) {
    const user = await User.save(request.validated());

    return user;
  }
}

Controller always ensures independence between request contexts, meaning that each time a request is sent to a controller, a new instance of the controller is created to handle that request. This helps avoid issues related to shared state between different requests.

Guessing HTTP Status Code

Laratype has the ability to automatically guess the appropriate HTTP status code based on the action performed in the controller. For example, when creating a new resource, Laratype will return a 201 Created status code. When deleting a resource, the 204 No Content status code will be used.

Controller method nameHTTP Status CodeDescription
index200List resources
create201Create resource
show200Show resource
update200Update resource
destroy204Delete resource

There are 2 ways to customize the HTTP Status Code in the controller:

  • response(): Use the response() helper to return a response with a custom status code.

  • UseStatusCode(): Use the UseStatusCode() decorator to specify the status code for a specific method in the controller.

IMPORTANT

Priority order: response() > UseStatusCode() > Guessing HTTP Status Code.

response()

  • Use the response() helper to return a response with a custom status code.
ts
import { Controller, response } from "@laratype/http";
import CreateUserRequest from "../requests/CreateUserRequest";
import { User } from "../../models/User";
export default class RegisterController extends Controller {
  async register(request: CreateUserRequest) {
    const user = await User.save(request.validated());

    return response(user, 201); 
  }
}

UseStatusCode()

  • Use the UseStatusCode() decorator to specify the status code for a specific method in the controller.
ts
import { Controller, UseStatusCode } from "@laratype/http";
import CreateUserRequest from "../requests/CreateUserRequest";
import { User } from "../../models/User";
export default class RegisterController extends Controller {
  @UseStatusCode(201) 
  async register(request: CreateUserRequest) {
    const user = await User.save(request.validated());

    return user;
  }
}

redirect()

You can use the redirect() method in the controller to redirect users to another URL.

ts
import { Controller, UseStatusCode } from "@laratype/http";
import CreateUserRequest from "../requests/CreateUserRequest";
import { User } from "../../models/User";
export default class RegisterController extends Controller {
  async register(request: CreateUserRequest) {
    const user = await User.save(request.validated());

    return redirect('/welcome'); 
  }
}

Commands

You can check the list of registered routes and related controllers using the Sauf command:

sh
$ npx sauf route:list
sh
$ pnpx sauf route:list
sh
$ yarn sauf route:list
sh
$ bun sauf route:list