LoginSignup
2
1

More than 1 year has passed since last update.

Laravelで、Target class does not exist.とエラーが出た時の対処法

Posted at

Laravel7とLaravel8で記法が異なることによりこちらのエラーが発生するようです。
バージョンアップ等を行う際は注意してください。

エラーが起こる箇所

routes直下のweb.phpです。

web.php
Route::resource('hoge', 'hogeController')->only([
    'index', 'show'
]);

Controllerの処理をindexとshowのみに限定するコードを書こうとしていたケースとなっていて、
web.phpに書く処理としてはベーシックなものになるので、
上記に似たようなコードであれば参考にしてください。

エラー文

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:877
    873 
    874         try {
    875             $reflector = new ReflectionClass($concrete);
    876         } catch (ReflectionException $e) {
   877             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    878         }
    879 
    880         // If the type is not instantiable, the developer is attempting to resolve
    881         // an abstract type such as an Interface or Abstract Class and there is

  1   [internal]:0
      Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))

      [2m+13 vendor frames [22m
  15  [internal]:0
      Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))

エラーが起こる原因

Laravel7とLaravel8のバージョンによる差異なようです。
Laravel8からは完全な名前空間を使用する必要があります。
名前空間とは

解決方法

コードを下記のように修正すれば完了です!

修正前

web.php
Route::resource('hoge', 'hogeController')->only([
    'index', 'show'
]);

修正後

web.php
Route::resource('hoge', 'App\Http\Controllers\hogeController')->only([
    'index', 'show'
]);

Laravelにおける各Controllerの名前空間は、
App\Http\Controllers\hogeController
ですが、名前空間についての理解が曖昧な方はまずは名前空間に関して学習してからの方が良いでしょう。

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