3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel10のmake:controllerで用意されている各typeを見てみる

Last updated at Posted at 2023-03-06

Laravel10のmake:contorller

Laravel10では

$ php artisan make:controller

とコントローラ名を入力せずにmake:controllerコマンドを打つと、

  What should the controller be named?
❯

という案内が表示されてコントローラー名を受け付け、さらに

  Which type of controller would you like: [empty]
  empty ........................................... 0
  api ............................................. 1
  invokable ....................................... 2
  resource ........................................ 3
  singleton ....................................... 4 

と、作ろうとしているコントローラーのある程度のひな形を用意してくれてそうです。

各ひな形がどんなものか見ていきます。

empty

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class EmptyController extends Controller
{
    //
}

空っぽですね。Illuminate\Http\Requestがあらかじめインポートされているくらいです。

api

apiを選ぶと追加でモデルを聞かれました。

  What model should this api controller be for? [none]
❯ 

今回はUserモデルを指定しました。

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class ApiController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, User $user)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(User $user)
    {
        //
    }
}

指定したApp\Models\Userがインポートされ、よくあるCRUDの各メソッドが空っぽで作成されています。showupdatedestroyではAPIのパスにidが含まれているであろうことからルートモデルバインディングで取得されるはずのUserが引数に加えられています。
APIリソースルートで指定するコントローラーのひな形のようです。

invokable

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class InvokableController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request)
    {
        //
    }
}

__invokeメソッドがあらかじめ作られています。
シングルアクションコントローラーのひな形のようです。

resource

resourceを選ぶと追加でさらにモデルを聞かれたので、Userを指定しました。

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class ResourceController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(User $user)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, User $user)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(User $user)
    {
        //
    }
}

apiと似ていますが、createeditが増えています。
リソースコントローラーのひな形のようです。

singleton

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class SingletonController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(User $user)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, User $user)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(User $user)
    {
        //
    }
}

シングルトンリソースコントローラーのひな形のようですが、ルートモデルバインディングが無いので引数になっているUserはどこから来るのかよくわからないです。あとcreateが何故かあります。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?