Routing parameters

Vapor has made it easy to pass a custom parameter type in the route and it will convert the text path name into the parameter we are requesting, as long as the class of the type we are requesting has implemented the Parameterizable protocol. The protocol consists of one property and one method:

public protocol Parameterizable {  static var uniqueSlug: String { get }      static func make(for parameter: String) throws -> Self}

Let's say we want to get an Item object from the database using an id that is passed as a parameter in the URL. We can get a specific item two ways. One is by extracting the id from the parameter and converting it to the Identifier type, and then finding the Item in the database where the identifier matches ...

Get Hands-On Full-Stack Development with Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.