15
8

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 3 years have passed since last update.

Laravel で Target class [Controller] does not exist.が出た時の対応

Posted at

事象

最近のLaravel (多分v8以降とかそのあたり)では、初期状態で以下のようなコードをroutes/web.phpに記述すると、あるはずのController Classを見つけられずエラーが発生する。

Route::get('/hoge', 'Controller@index');
Illuminate\Contracts\Container\BindingResolutionException
Target class [Controller] does not exist.

http://laravel.internal/hoge

Illuminate\Container\Container::build
htdocs\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php:811

どうやらContollerの初期位置を見失うようになったらしい。

対応

App/Providers/RouteServiceProvider.php の$namespace のコメントアウトを外す

    /**
     * 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'; // ここがデフォルトでコメントアウトされるようになった

#参考等

https://kawax.biz/laravel8-routing/
(ここまで書いて理由とか気になってググったらこのブログで全部解決していた)

15
8
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
15
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?