0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP備忘録:Laravel学習中に遭遇したAuthController問題、私の場合はこう直した記録メモ

Posted at

はじめに

Laravel の開発中に、AuthController に関するエラー("Class "App\Http\Controllers\AuthController" does not exist")が発生したので、原因と対処方法を備忘録としてまとめておきます。

個人の備忘録程度の走り書きとなっておりますが、温かい目で見守っていただければ幸いです。

書こうと思ったきっかけ

個人的に PHP のキャッチアップを目的として Laravel を学習しており、その過程でよくあるエラーに遭遇しました。Laravel 初学者として、後から振り返れるように記録しておきます。

内容

エラー内容は次の通りです:

Illuminate\Contracts\Container\BindingResolutionException
Target class [App\Http\Controllers\AuthController] does not exist.

このエラーは Laravel が AuthController クラスを認識できていないときに発生します。

実際につまづいた画面

Screenshot 2025-05-10 at 13.00.16.png

実際にはファイルが存在しているにもかかわらず、以下のような原因が考えられました。

対処ステップまとめ

1. AuthController.php の中身が正しいか確認

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    public function showRegisterForm()
    {
        return view('auth.register');
    }

    public function register(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);

        User::create([
            'name' => $validated['name'],
            'email' => $validated['email'],
            'password' => Hash::make($validated['password']),
        ]);

        return redirect('/register')->with('success', 'ユーザー登録が完了しました');
    }
}

※ 同じファイルに namespaceclass が重複定義されていないかも確認すること。

2. ファイル名の確認(大文字小文字も)

ls -l src/app/Http/Controllers/AuthController.php

3. オートロード再生成とキャッシュ削除

docker compose exec php-app composer dump-autoload
docker compose exec php-app php artisan config:clear
docker compose exec php-app php artisan route:clear
docker compose exec php-app php artisan view:clear

4. ルート確認

docker compose exec php-app php artisan route:list

ここで /register が表示されていれば、Laravel 側でルートが認識されていることになります。

実際の画面

 ~/Desktop/moromoro/php-docker  (git)-[main]-  docker compose exec php-app php artisan route:list

WARN[0000] /Users/hondaakiratarou/Desktop/moromoro/php-docker/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion 

  GET|HEAD   / .................................................................. 
  GET|HEAD   hello ........................................ HelloController@index
  POST       hello ......................................... HelloController@post
  GET|HEAD   message ................................... MessageController@create
  POST       message .................................... MessageController@store
  GET|HEAD   register ........................... AuthController@showRegisterForm
  POST       register ................................... AuthController@register
  GET|HEAD   storage/{path} ....................................... storage.local
  GET|HEAD   up ................................................................. 

                                                               Showing [9] routes

まとめ

Laravel のエラーは、構文ミスやファイル認識のズレから起きることが多いです。

特に、クラス名とファイル名、名前空間の整合性、キャッシュのリセットは重要なチェックポイントです。

Laravel 初学者の方の参考になれば幸いです...!

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?