> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# プレリュード

> Laravel for Rust のプレリュードに含まれる型とメソッド、それ以外のインポート方法、テスト用プレリュードを解説します。

## はじめに

アプリケーションのほとんどのファイルは、リクエスト、レスポンス、リダイレクト、ルーター、ビューといった、同じ少数の Laravel の型を使います。それぞれをインポートする代わりに、ファイルの先頭で Laravel のプレリュードをインポートできます。

```rust theme={null}
use laravel::prelude::*;

#[laravel::input]
pub struct Rename {
    name: Option<String>,
}

pub async fn store(request: Request) -> laravel::Result<Redirect> {
    let name = request.parse::<Rename>()?.name.or_abort(422)?;

    // ...

    Ok(Redirect::back())
}
```

このドキュメントのほとんどの例は、このインポートを前提としています。

## プレリュードに含まれるもの

プレリュードには、ほとんどのハンドラーとルートファイルが使う型が含まれます。

* `Request`、`Response`、`Redirect`、`Json`、`Query`、`StatusCode`
* ルート用の `Router` と `Params`
* `Session`、`Inject`、`Application`
* [検証済み入力](https://laravel.com/rust/docs/validation#validation-quickstart)用の `Valid` と、[ログイン中のユーザー](https://laravel.com/rust/docs/authentication#retrieving-the-logged-in-user)用の `Authenticated`
* [ルート URL](https://laravel.com/rust/docs/urls#urls-for-routes)と[リダイレクト](https://laravel.com/rust/docs/responses#redirecting-to-routes)用の `route!` マクロと `redirect!` マクロ
* [ビュー](https://laravel.com/rust/docs/views)用の `view` 関数と `data!` マクロ
* [モデルカラム](https://laravel.com/rust/docs/models#enum-columns)や[フォームフィールド](https://laravel.com/rust/docs/validation#enum-and-id-fields)で使う、独自の列挙型や単一フィールド構造体用の `Enum` derive と `Newtype` derive

すでに使っている型へのメソッド追加もあります。

* 結果に対する `context` と `with_context`（[エラーへのコンテキスト追加](https://laravel.com/rust/docs/errors#adding-context)用）
* オプションに対する `or_abort`
* [認可](https://laravel.com/rust/docs/authorization#checking-abilities)用の `can` と `authorize`
* [フィーチャーフラグ](https://laravel.com/rust/docs/feature-flags#checking-features)用の `features`
* [データベースバリデーションルール](https://laravel.com/rust/docs/validation#database-records)用の `Rule::unique` と `Rule::exists`
* `snake` などの[文字列ヘルパー](https://laravel.com/rust/docs/strings)と、`is_past` などの[日付ヘルパー](https://laravel.com/rust/docs/time)
* `5.minutes()` のようなスパン
* レスポンスに対する `with_cookie` と `without_cookie`
* [eager load](https://laravel.com/rust/docs/model-relationships#eager-loading)用の、モデルのリストに対する `load` と、リレーションに対する `with`

これらのメソッドはトレイト由来のため、プレリュードをインポートしたファイルでのみ機能します。`or_abort` のようなメソッドが存在しないとコンパイラーが言う場合は、ファイルのインポートを確認してください。

## それ以外のインポート

それ以外はすべて、`laravel::foundation::ServiceProvider` や `laravel::view::View` のように、それぞれのモジュールからインポートします。このドキュメントの各セクションの最初の例はそのインポートを示し、それ以降の例はそれを前提としています。

プレリュードは、Rust 自身のものを置き換えないよう、Laravel の `Result` 型と `Error` 型を除外しています。上のハンドラーのように、`laravel::Result`、`laravel::Error` と書いてください。

## テスト用プレリュード

テストでは、代わりにテスト用プレリュードをインポートします。プレリュードのすべてに加えて、アプリケーションを検証するためにテストが必要とするものが含まれます。

* アプリケーションへリクエストを送る `get`、`post` などのリクエストメソッド
* コンソールコマンドを実行する `command`
* `assert_database_has` などのデータベースアサーション
* JSON リクエストボディとアサーション用の `json!` マクロ

この1つのインポートで、テストはリクエストを送り、アプリケーションが保存したものを確認できます。

```rust theme={null}
use laravel::testing::prelude::*;

#[laravel::test]
async fn messages_can_be_posted(app: Application) {
    app.post("/messages")
        .json(json!({ "body": "Hello!" }))
        .await
        .assert_redirect();

    app.assert_database_has("messages", json!({ "body": "Hello!" })).await;
}
```

自分のテストを書く準備ができたら、[テストのガイド](https://laravel.com/rust/docs/testing)が、ログイン中のユーザーとしての操作やデータベースの確認から、HTTP リクエストのフェイクや時間移動まで、テストクライアントにできることのすべてを案内します。


## Related topics

- [Bladeテンプレート](https://kawax.biz/jp/blade.md)
- [エンジン API モード：ソング（歌声音声合成） - VOICEVOX for Laravel](https://kawax.biz/jp/packages/laravel-voicevox/engine-song.md)
- [クライアントモード：ソング（歌声音声合成） - VOICEVOX for Laravel](https://kawax.biz/jp/packages/laravel-voicevox/client-song.md)
- [ネイティブモード：ソング（歌声音声合成） - VOICEVOX for Laravel](https://kawax.biz/jp/packages/laravel-voicevox/native-song.md)
- [Laravelとは](https://kawax.biz/jp/introduction.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.