Skip to main content

HttpContext

Struct HttpContext 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional 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: Container

Request-scoped view of the application’s services.

§auth: Auth

Who the request is. Anonymous unless a guard set it.

§config: Config

Merged configuration: luxid.toml with the environment over it.

§session: Session

The request’s session. Detached unless Auth::session() is active.

§inertia: InertiaRequest

Inertia protocol state. Detached unless the Inertia middleware ran.

Implementations§

Source§

impl HttpContext

Source

pub fn authorize<T>( &self, policy: impl FnOnce(&Auth, &T) -> bool, subject: &T, ) -> Result<()>

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

pub fn can<T>( &self, policy: impl FnOnce(&Auth, &T) -> bool, subject: &T, ) -> bool

The same question without the consequence, for deciding what to render.

Source§

impl HttpContext

Source

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.

Trait Implementations§

Source§

impl Debug for HttpContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more