Laravel

【Laravel8】「Target class [XXXController] does not exist.」が発生するときの対処法!

あきぞらです。

新規でLaravelのプロジェクトを作成するときは、Laravel8を使います。

あるとき、Laravel8を使ってルーティングの設定をしていると、

以下のようなエラーが発生してしまいました。

 Illuminate\Contracts\Container\BindingResolutionException 

Target class [TestController] does not exist.

at \Users\akizora\test\vendor\laravel\framework\src\Illuminate\Container\Container.php:835
  831▕
  832▕         try {
  833▕             $reflector = new ReflectionClass($concrete);
  834▕         } catch (ReflectionException $e) {
➜ 835▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
  836▕         }
  837▕
  838▕         // If the type is not instantiable, the developer is attempting to resolve
  839▕         // an abstract type such as an Interface or Abstract Class and there is

今回はこのエラーの対処方法について紹介していきます。

Laravel8以降ではルーティングの書き方が少し異なる

Laravel8以降では、ルーティングの書き方が少し異なるようです。

これまでは以下のような記述でうまくいけました。

以下は、api.phpです。

<?php

use Illuminate\Http\Request;

Route::resource('test', 'TestController')->only(['store']);

Laravel8以降では、以下のように書き方を変える必要があります。

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestController;

Route::resource('test', TestController::class, ['only' => ['store']]);

「ちょっと書き方が変わっていやだな~」と思うかもしれませんが、

個人的にはLaravel8の書き方はuseする個所は増えますがシンプルでけっこう好きです。

バージョンアップなどの際には注意が必要です。

-Laravel

Copyright© あきぞらてっく , 2025 All Rights Reserved Powered by AFFINGER5.