Laravel8のコントローラーを定義する場合には、下記のエラーが発生することがあります。
その解決方法を、本記事では解説しています。
エラー内容
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |   Illuminate\Contracts\Container\BindingResolutionException   Target class [XxxxxController] does not exist.   at vendor/laravel/framework/src/Illuminate/Container/Container.php:879     875?     876?         try {     877?             $reflector = new ReflectionClass($concrete);     878?         } catch (ReflectionException $e) {   ? 879?             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);     880?         }     881?     882?         // If the type is not instantiable, the developer is attempting to resolve     883?         // 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))       +13 vendor frames   15  [internal]:0       Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route)) | 
このエラーの原因として、コントローラークラスが見つからないことが原因となります。
Laravel7までは、ルーティングを記述する routes/web.phpファイルにデフォルトのコントローラには、
名前空間(App\\Http\\Controllers)が適用されていましたが、Laravel8では、このデフォルトの設定がなくなりました。
そのため、エラー対策としては、クラスが見つかるように、route.php の記述を変更する必要があります。
Laravel8系の場合には、Laravel7系以前とは、記述方法が異なります。
そのため、いちばん簡単な解決方法は、下記のような方法となります。
利用するクラスの完全な名前を、use宣言することで、エラーが発生しなくなります。
routes/web.php
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\xxx\XxxxController; ※↑ 利用するコントローラークラスを定義します。 /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () {     return view('welcome'); }); Route::get('/sample', [ SampleController::class, 'index'] ); ※Routeの定義では、classを指定する記述になります。 | 
 
  
  
  
   
 
         
         
         
         
         
        
