vapor-docs/docs/basics/controllers.md

1.8 KiB

Controllers

Controllers are a great way to organize your code. They are collections of methods that accept a request and return a response.

A good place to put your controllers is in the Controllers folder.

Overview

Let's take a look at an example controller.

import Vapor

struct TodosController: RouteCollection {
    func boot(routes: RoutesBuilder) throws {
        let todos = routes.grouped("todos")
        todos.get(use: index)
        todos.post(use: create)

        todos.group(":id") { todo in
            todo.get(use: show)
            todo.put(use: update)
            todo.delete(use: delete)
        }
    }

    func index(req: Request) async throws -> String {
        // ...
    }

    func create(req: Request) throws -> EventLoopFuture<String> {
        // ...
    }

    func show(req: Request) throws -> String {
        guard let id = req.parameters.get("id") else {
            throw Abort(.internalServerError)
        }
        // ...
    }

    func update(req: Request) throws -> String {
        guard let id = req.parameters.get("id") else {
            throw Abort(.internalServerError)
        }
        // ...
    }

    func delete(req: Request) throws -> String {
        guard let id = req.parameters.get("id") else {
            throw Abort(.internalServerError)
        }
        // ...
    }
}

Controller methods should always accept a Request and return something ResponseEncodable. This method can be asynchronous or synchronous (or return an EventLoopFuture)

!!! note EventLoopFuture whose expectation is ResponseEncodable (i.e, EventLoopFuture<String>) is also ResponseEncodable.

Finally you need to register the controller in routes.swift:

try app.register(collection: TodosController())