LoginSignup
6
2

More than 3 years have passed since last update.

laravel 8 Target classが見つからない件

Last updated at Posted at 2020-12-17

laravel 8が今年の9月8日にリリースされてたことを知り早速使ってみようとした矢先、予期せぬエラーに出鼻をくじかれたのでどこかの誰かのためになればと思い一応メモしておきます。

やったこと

検証したいことがありroute/api.phpに下記のコードを記述。

Route::post('/login', 'Auth\LoginController@login');

そしてapp/Http/Controllers/Auth配下にLoginController.phpを配置して、loginメソッドを記述しました。念のため程度にphp artisan route:listで設定したルーティングがきちんと認識されているか確認しようとしたらエラーが出てしまった。

エラー内容

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [Auth\LoginController] does not exist.

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

なぜかLoginControllerクラスが見つからない。。。。

結論

なぜかapp/Providers/RouteServiceProvider.phpで定義されているデフォルトのネームスペースがlaravel 7までコメントアウトされていなかったのにlaravel 8からコメントされるようになってただけでした。
コメントアウト外せば無事解決しました。

laravel 7

    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

laravel 8

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\\Http\\Controllers';

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