#[non_exhaustive]pub struct HttpContext {
pub request: Request,
pub response: Response,
pub params: Params,
pub extensions: Extensions,
pub services: Container,
pub auth: Auth,
pub config: Config,
pub session: Session,
pub inertia: InertiaRequest,
}Expand description
Everything an action needs, owned.
#[non_exhaustive] is load-bearing: it forces downstream crates to write
.. in struct patterns (E0638), which makes adding fields — auth, db,
inertia — permanently non-breaking.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.request: Request§response: Response§params: Params§extensions: Extensions§services: ContainerRequest-scoped view of the application’s services.
auth: AuthWho the request is. Anonymous unless a guard set it.
config: ConfigMerged configuration: luxid.toml with the environment over it.
session: SessionThe request’s session. Detached unless Auth::session() is active.
inertia: InertiaRequestInertia protocol state. Detached unless the Inertia middleware ran.
Implementations§
Source§impl HttpContext
impl HttpContext
Enforce a policy, or fail with a 403.
A policy is any function of (&Auth, &T) -> bool, so it is an ordinary
function in an ordinary impl — no trait to implement, no registry to
populate:
ctx.authorize(PostPolicy::update, &post)?;Returning bool rather than Result keeps policies about permission.
A policy that needs to fail some other way — a missing row is a 404, not
a 403 — should say so before the authorization check, not inside it.
Source§impl HttpContext
impl HttpContext
Sourcepub fn inertia(self, component: &str, props: impl Serialize) -> Result<Response>
pub fn inertia(self, component: &str, props: impl Serialize) -> Result<Response>
Render an Inertia page.
async fn index(ctx: HttpContext) -> Result<Response> {
let todos = Todo::owned_by(ctx.auth.id()?).paginate(1, 50).await?;
ctx.inertia("Todos/Index", json!({ "todos": todos }))
}component is a path into the client’s page directory —
"Todos/Index" resolves to resources/js/Pages/Todos/Index.jsx.
The same call serves both a fresh browser load (an HTML shell) and an Inertia navigation (JSON). The action does not know or care which.
Consumes the context so any headers or cookies already set on
ctx.response are preserved.