MeWrite Docs

Class not found

LaravelでクラスやFacadeが見つからない場合に発生するエラー

概要

Class not found は、LaravelでPHPクラスをオートロードできない場合に発生するエラーです。名前空間の設定ミス、Composerのオートロード未更新、ファイル名の不一致などが原因です。

エラーメッセージ

Error: Class 'App\Services\UserService' not found
Target class [App\Http\Controllers\UserController] does not exist.
Class 'App\Models\User' not found in /var/www/html/app/Http/Controllers/UserController.php on line 15

原因

  1. オートロード未更新: 新しいクラスを追加後、composer dump-autoloadを実行していない
  2. 名前空間の不一致: ファイル内の名前空間とディレクトリ構造が一致していない
  3. ファイル名の不一致: クラス名とファイル名が異なる(PSR-4違反)
  4. use文の欠落: クラスをインポートしていない
  5. キャッシュの問題: 設定やルートのキャッシュが古い

解決策

1. Composerオートロードを再生成

1
2
3
4
5
6
7
8
9
# オートロードファイルを再生成
composer dump-autoload

# 最適化付きで再生成
composer dump-autoload -o

# 全キャッシュをクリアして再生成
php artisan optimize:clear
composer dump-autoload

2. 名前空間とディレクトリ構造を確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// ファイル: app/Services/UserService.php
// 名前空間はディレクトリ構造と一致させる

// ❌ 間違い
namespace App\Service; // Services ではなく Service

// ✅ 正しい
namespace App\Services;

class UserService
{
    // ...
}

3. ファイル名とクラス名を一致させる

# PSR-4規約: クラス名とファイル名は完全一致(大文字小文字含む)

❌ app/Services/userservice.php  → class UserService
✅ app/Services/UserService.php  → class UserService

4. use文でクラスをインポート

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// ❌ 間違い
class UserController extends Controller
{
    public function index()
    {
        $users = User::all(); // Class 'User' not found
    }
}

// ✅ 正しい
use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $users = User::all();
    }
}

5. Laravelキャッシュをクリア

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 設定キャッシュをクリア
php artisan config:clear

# ルートキャッシュをクリア
php artisan route:clear

# ビューキャッシュをクリア
php artisan view:clear

# 全キャッシュをクリア
php artisan optimize:clear

6. composer.jsonのautoload設定を確認

1
2
3
4
5
6
7
8
9
{
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    }
}

新しいディレクトリを追加した場合:

1
2
3
4
5
6
7
8
{
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Modules\\": "modules/"
        }
    }
}

設定変更後は必ず実行:

1
composer dump-autoload

7. Facadeが見つからない場合

1
2
3
4
5
6
7
// config/app.php の aliases を確認
'aliases' => [
    'Auth' => Illuminate\Support\Facades\Auth::class,
    'Cache' => Illuminate\Support\Facades\Cache::class,
    // カスタムFacadeを追加
    'MyService' => App\Facades\MyService::class,
],

または use文で直接インポート:

1
2
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

よくあるケース

Modelの場所が変わった(Laravel 8+)

1
2
3
4
5
// Laravel 7以前
use App\User;

// Laravel 8以降
use App\Models\User;

ServiceProviderでのバインディング

1
2
3
4
5
6
7
8
// app/Providers/AppServiceProvider.php
public function register()
{
    $this->app->bind(
        \App\Contracts\UserRepositoryInterface::class,
        \App\Repositories\UserRepository::class
    );
}

Laravel の他のエラー

最終更新: 2025-12-08