Path patterns
A path pattern is a string that determines which requests are matched by a given route based on their path.
Static paths
The simplest case is a static path, a path pattern that matches a single, fixed path:
use pavex::blueprint::{router::GET, Blueprint};
use pavex::f;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/greet", f!(crate::routes::greet));
bp
}
It will only match requests with a path that is exactly equal to /greet
.
Path parameters
Static paths are fairly limited. The real power of path patterns comes from their ability to match dynamic paths:
use pavex::blueprint::{router::GET, Blueprint};
use pavex::f;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/greet/{name}", f!(crate::routes::greet));
bp
}
The {name}
segment is a route parameter.
It matches everything after /greet/
, up to the next /
or the end of the path.
It matches, for example, /greet/Ursula
and /greet/John
. It won't match /greet/
though!
You can have multiple path parameters in a single path pattern, as long as they are separated by a static segment:
use pavex::blueprint::{router::GET, Blueprint};
use pavex::f;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(
GET,
"/greet/{first_name}/{last_name}",
f!(crate::routes::greet),
);
bp
}
Catch-all parameters
Normal path parameters match a single path segment—they stop at the next /
or at the end of the path.
You can use the *
character to craft a catch-all route parameter. It matches the rest of the path, regardless of its contents:
use pavex::blueprint::{router::GET, Blueprint};
use pavex::f;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/greet/{*details}", f!(crate::routes::greet));
bp
}
{*details}
matches everything after /greet/
, even if it contains /
characters.
/greet/*details
matches, for example, /greet/Ursula
and /greet/John
, but it also matches /greet/Ursula/Smith
and /greet/John/Doe
.
To avoid ambiguity, you can have at most one catch-all parameter per path pattern and it must be at the end of the path pattern.
Accessing path parameters
Path parameters are not discarded after a request has been routed. You can access their values from your request handler or from middlewares.
Check out the "Path parameters" guide for more details.