8
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

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

事象

最近の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/
(ここまで書いて理由とか気になってググったらこのブログで全部解決していた)

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
8
Help us understand the problem. What are the problem?