vapor-docs/3.0/docs/routing/parameters.md

44 lines
1.2 KiB
Markdown

# Parameters
Parameters are a registered type that can be initialized from a String.
They can be part of a [Route](route.md), and be extracted from [Requests](../http/request.md) that are called in that Route.
## Creating custom parameters
To create a custom parameter type, simply conform to `Parameter` and implement the conversion function `make` and a unique slug.
In this example, the `User` class will be initialized from a parameter that represents it's identifier.
We recommend prefixing custom Parameter identifiers.
```swift
class User : Parameter {
var username: String
// The unique (prefixed) identifier for this type
static var uniqueSlug = "my-app:user"
// Creates a new user from the raw `parameter`
static func make(for parameter: String, in request: Request) throws -> User {
return User(named: parameter)
}
init(named username: String) {
self.username = username
}
}
```
## Using (custom) parameters
After conforming a type to `Parameter` you can access it's static property `parameter` as part of a path.
```swift
router.on(.get, to: "users", User.parameter, "profile") { request in
let user = try request.parameters.next(User.self)
// Return the user's Profile sync or async (depending on the router)
}
```