A Laravel(API) - user000422/0 GitHub Wiki

設定

**■APIのURLプレフィックスを変更したい。** 参考サイト: https://www.blog.danishi.net/2020/05/11/post-3608/

ルーティング

routes/api.php

プログラミング

公式リクエスト: https://readouble.com/laravel/10.x/ja/requests.html 204はレスポンスボディがないためJavascriptでうまく判定しないとボディなしエラーになる

// Controller

public function index()
{
    // 例)ユーザ情報取得
    $users = User::all();

    // 200返却 基本型 JSON response
    return response()->json([
        'status' => true,
        'users' => $users,
    ], 200);

    // 204返却(更新や削除の成功)
    return response()->noContent();

    // 201返却(登録の成功)※noContentのように独自メソッドは用意されていない
    return response()->json(null, 201);
}

ダウンロード

// Controller

// 参考書型
public function download()
{
    $response = Response::download('/path/file.pdf');

    // 基本型 JSON response
    $response = response()->download(
        '/path/file.pdf',
        'users.pdf',
        [
            'content-type' => 'application/pdf',
        ]
    );

    return $response;
}