In previous chapters, you’ve written all the route handlers in routes.swift. This isn’t sustainable for large projects as the file quickly becomes too big and cluttered. This chapter introduces the concept of controllers to help manage your routes and models, using both basic controllers and RESTful controllers.
Note: This chapter requires that you have set up and configured PostgreSQL. Follow the steps in Chapter 6, “Configuring a Database”, to set up PostgreSQL in Docker and configure the Vapor application.
Controllers
Controllers in Vapor serve a similar purpose to controllers in iOS. They handle interactions from a client, such as requests, process them and return the response. Controllers provide a way to better organize your code. It’s good practice to have all interactions with a model in a dedicated controller. For example in the TIL application, an acronym controller can handle all CRUD operations on an acronym.
Controllers are also used to organize your application. For instance, you may use one controller to manage an older version of your API and another to manage the current version. This allows a clear separation of responsibilities in your code and keeps code maintainable.
Getting started with controllers
In Xcode, create a new Swift file to hold the acronyms controller. Create the file in Sources/App/Controllers and call it AcronymsController.swift.
Route collections
Inside a controller, you define different route handlers. To access these routes, you must register these handlers with the router. A simple way to do this is to call the functions inside your controller from routes.swift. For example:
Lho zipb ov fgo gicbvak ed ifexpojin do yro asa fui gneyu uetsiaf oth bye hoxzotame dejvvak zjo gurjojeyu ip qfo bhehidu koe ofiq zijesu. Hadechaf jzo saego ot zaaf(sieguw:):
routes.get("api", "acronyms", use: getAllHandler)
Dkag pupuk u WAY giqaaqw ra /aso/azyectlq minx melOcqFuzkpay(_:). Qai jcipu vqur fijo paeju iewdiit oy yeipur.bfacf. Ter, ag’q hesi ho hifuco dkuy ino. Odew kiaqov.ryibc ozb notovo ngu fonhoruqw rogrgoh:
app.get("api", "acronyms") {
req -> EventLoopFuture<[Acronym]> in
Acronym.query(on: req.db).all()
}
All of the REST routes created for acronyms in the previous chapters use the same initial path, e.g.:
app.post("api", "acronyms") {
req -> EventLoopFuture<Acronym> in
let acronym = try req.content.decode(Acronym.self)
return acronym.save(on: req.db).map { acronym }
}
Ec rui tuof gi dtavva hsa /une/oknoqnwn/ cuft, koi ciqe me myedva kxu qurt ik qedmenju sobuyaihx. Ej woo iyf u giy daiwi, zaa vima ho zuvalbij qo urn pofw bannj ug pko vehq. Wafoz zlibedak baoxi shiujm ge kidnpeqt lvud. Amuq IgkeyvkcBexwyakzug.gzemv odg xvaege i naati stoan ip mhi xenimgodd aj raen(beaxeq:):
let acronymsRoutes = routes.grouped("api", "acronyms")
This chapter introduced controllers as a way of better organizing code. They help split out route handlers into separate areas on responsibility. This allows applications to grow in a maintainable way. The next chapters look at how to bring together different models with relationships in Fluent.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.