0
0

【Laravel】Illuminateにある主要メソッド Http編

Last updated at Posted at 2024-07-25

言語

  • Laravel8

Illuminateとは

  • Laravelが提供する様々なサービスやコンポーネントを構成するための基盤を提供している。
  • サービスやコンポーネントはIlluminate名前空間内に整理されている

どこにあるか?

プロジェクトディレクトリ\vendor\laravel\Framework\src\Illuminate\~

Illuminate\Http

  • リクエストとレスポンスの操作を提供する。

Illuminate\Http\Request

  • RequestクラスはHttpリクエストに関する情報を管理するためのメソッドを提供。

all()

  • 全ての入力データを取得
     $allData = $request->all();
    

input($key = null, $defalt = null)

  • 指定されたキーの入力データを取得
     $nameData = $request->input('name');
    

has($key)

  • 指定されたキーの入力データが存在するか確認する
     if ($request->has('name')) {
     
     }
    

only($keys)

  • 指定されたキーの入力データのみを取得
     $inputData = $request->only(['name', 'email']);
    

except($keys)

  • 指定されたキー以外の全ての入力データを取得
     $inputData = $request->except(['password']);
    

file($key = null, $default = null)

  • 指定されたキーのファイルを取得
     $file = $request->file('image');
    

query($key = null, $default = null)

  • クエリストリングのパラメータを取得
     $page = $request->query('page');
    

クエリストリングとは?
URLの「?」以降にキーと値のペアとして追加の情報を提供するために使われるパラメータのこと。

URL
http://example.com/search?name=test&page=5
  • 上記の場合、クエリストリング部分は'name=test&page=5'です。
  • 'name''test'という値。
  • 'page''5'という値。

method()

  • Httpリクエストのメソッドを取得
     $method = $request->method();
    

isMethod($method)

  • リクエストメソッドが指定されたメソッドかどうかを確認
     if ($request->isMethod('post')) {
     
     }
    

ajax()

  • リクエストがAJAXリクエストかどうかを確認
     if ($request->ajax()) {
     
     }
    

ajaxとは?
AJAX = Asynchronous JavaScript and XML
ウェブページがサーバーと非同期にデータをやり取りするための技術。
ページ全体を再読み込みすることなく、部分的にデータを更新することができる。

Illuminate\Http\Response

  • ResponseクラスはHttpレスポンスを管理するためのメソッドを提供。

json($data = [], $status = 200, array $headers = [], $options = 0)

  • JSON形式のレスポンスを作成する
     return response()->json(['name' => 'qiita', 'state' => 'teacher']);
    

jsonとは?
JSON = JavaScript Object Notation
データの表現方法の一つで、軽量で読みやい。
JSON形式の基本的な構造は、オブジェクト{}・配列[]・値。

JSONの例
{
    "person": {
        "name": "Qiita",
        "email": "test@example.com",
        "address": {
            "street": "123 Main St",
            "city": "Tokyo"
        },
        "hobbies": ["reading", "gaming"],
        "isMale": true
    }
}

download($file, $name = null, array $headers = [], $disposition = 'attachment')

  • 指定されたファイルのダウンロードレスポンスを作成する
     return response()->download($pathToFile);
    

make($content = '', $status = 200, array $headers = [])

  • 新しいレスポンスインスタンスを作成する
     return response()->make('Hello Qiita', 200);
    

redirectTo($path, $status = 302, $headers = [], $secure = null)

  • 指定されたURLへのリダイレクトレスポンスを作成する。
     return redirect()->to('/index');
    

私のIlluminate関連記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0