Skip to content

The Prelude

Getting Started

The Prelude

Introduction

Most files in your application use the same handful of Laravel types: requests, responses, redirects, the router, and views. Instead of importing each one, you may import Laravel's prelude at the top of the file:

1use laravel::prelude::*;
2 
3#[laravel::input]
4pub struct Rename {
5 name: Option<String>,
6}
7 
8pub async fn store(request: Request) -> laravel::Result<Redirect> {
9 let name = request.parse::<Rename>()?.name.or_abort(422)?;
10 
11 // ...
12 
13 Ok(Redirect::back())
14}
1use laravel::prelude::*;
2 
3#[laravel::input]
4pub struct Rename {
5 name: Option<String>,
6}
7 
8pub async fn store(request: Request) -> laravel::Result<Redirect> {
9 let name = request.parse::<Rename>()?.name.or_abort(422)?;
10 
11 // ...
12 
13 Ok(Redirect::back())
14}

Most examples in these docs assume this import.

What the prelude includes

The prelude includes the types most handlers and route files use:

  • Request, Response, Redirect, Json, Query, and StatusCode
  • Router and Params for your routes
  • Session, Inject, and Application
  • Valid for validated input, and Authenticated for the logged-in user
  • The route! and redirect! macros for route URLs and redirects
  • The view function and data! macro for views
  • The Enum and Newtype derives, for your own enums and single-field structs in model columns and form fields

It also adds methods to types you already use:

These methods come from traits, so they only work in files that import the prelude. If the compiler says a method such as or_abort doesn't exist, check the file's imports.

Importing everything else

Import everything else from its own module, such as laravel::foundation::ServiceProvider or laravel::view::View. The first example in each section of these docs shows its imports, and the examples after it assume them.

The prelude leaves out Laravel's Result and Error types so they don't replace Rust's own. Write them as laravel::Result and laravel::Error, as the handler above does.

The testing prelude

In your tests, import the testing prelude instead. It includes everything in the prelude, plus what your tests need to exercise your application:

  • get, post, and the other request methods, to send requests to your application
  • command, to run your console commands
  • assert_database_has and the other database assertions
  • The json! macro, for JSON request bodies and assertions

With that one import, a test can send a request and check what your application stored:

1use laravel::testing::prelude::*;
2 
3#[laravel::test]
4async fn messages_can_be_posted(app: Application) {
5 app.post("/messages")
6 .json(json!({ "body": "Hello!" }))
7 .await
8 .assert_redirect();
9 
10 app.assert_database_has("messages", json!({ "body": "Hello!" })).await;
11}
1use laravel::testing::prelude::*;
2 
3#[laravel::test]
4async fn messages_can_be_posted(app: Application) {
5 app.post("/messages")
6 .json(json!({ "body": "Hello!" }))
7 .await
8 .assert_redirect();
9 
10 app.assert_database_has("messages", json!({ "body": "Hello!" })).await;
11}

When you're ready to write tests of your own, the testing guide walks you through everything the test client can do, from acting as a logged-in user and checking your database to faking HTTP requests and traveling through time.