Map URLs in web.rs and api.rs

Choose the web or api route file, generate links from handler functions, and group a prefix with middleware.

This course still sits on the 0.x beta. Check the routing guide that matches your installed crate if a macro name no longer compiles.

src/app.rs registers the two files you will use most:

.with_routing(|routing, _| {
    routing
        .web(routes::web::routes)
        .api(routes::api::routes);
})

web attaches the session group. api is stateless and prefixes every URI with /api, so route.get("/notes", notes::index) in src/routes/api.rs answers /api/notes. Change that prefix with api_prefix when you need /api/v1:

routing.api(routes::api::routes).api_prefix("/api/v1");

A third registration, routes, adds neither sessions nor CSRF nor a prefix. Use it for something like a signed webhook, and attach the middleware yourself. Field Notes does not need that file yet.

The router has get, post, put, patch, delete, and options. A GET route also answers HEAD. on takes a list of methods. any answers every method.

Add the notebook’s browser routes next to the greeting. The handlers arrive in the next lesson; for this step, point the URIs at functions you are about to write, or keep a closure until that file exists. The shape from the routing guide is:

use crate::http::handlers::notes;

pub fn routes(route: &mut Router) {
    route.get("/notes", notes::index);
    route.get("/notes/{note}", notes::show);
    route.post("/notes", notes::store);
}

{note} is a route parameter. For a Note model, Laravel binds that segment to the model. You will declare the model in the data course. Until then, do not add {note} unless the handler accepts something else, such as Params<i64>. A missing model becomes a 404, not a 422. Validation is a different failure, and it belongs on the input struct.

Links go through the handler, not a string you hope stays in sync:

let url = route!(notes::show, &note)?;
let response = redirect!(notes::show, &note);

Both macros are in the prelude. Renaming the path does not leave a stale string behind, and removing the handler is a compile error at every route! and redirect!. Redirect and view routes that have no handler are the exception: link to those by URI. route.redirect("/here", "/there") is a 302. permanent_redirect is a 301.

A page with no data of its own can skip the handler. route.view("/about", "about") renders a view. route.inertia("/pricing", "Pricing") renders an Inertia component. Field Notes will use real handlers, because the notebook has data.

A prefix and a middleware group

Authenticate is the documented middleware that sends guests to the login page (or returns 401 for a request that expects JSON). You do not have a login page yet. The group is still worth registering so the later access course has a place to hang note edits:

use laravel::auth::Authenticate;

route.prefix("/notes").middleware(Authenticate).group(|route| {
    route.get("/", notes::index);
    route.post("/", notes::store);
});

Middleware on a group runs around every route inside it. A tuple runs several, in order: middleware((Authenticate, RequireVerifiedEmail)). Field Notes does not require verified email in this series. The verification guide is there when you want that wall.

List the result:

laravel route:list --path=notes -v

You should be able to point at which routes have the session group, which would sit under /api if you had copied them into api.rs, and which ones sit behind Authenticate.

Check your work

Add GET /notes in web.rs and GET /notes in api.rs. Confirm route:list shows /notes and /api/notes.

Back to roadmap