スポンサーリンク

【Laravel】コントローラを作成してアクセスするとTarget class [クラス名] does not exist.となる

Laravel 環境を新たに構築して、試しに GET API を作って動かしてみたのですが、クラスが見つからないというエラーが出たので、その解決方法を残しておきたいと思います。

Laravel のバージョンは 8 系です。

$ php artisan --version
Laravel Framework 8.46.0

出たのはこのようなエラー。

Laravel 新規インストール後に行った作業としては、

$ php artisan make:controller HelloController

でコントローラクラスを一つ作成し、

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloController extends Controller
{
    public function index () 
    {
	return 'Hello!';
    }
}

ルートの設定を行っただけです。

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::get('/index', 'HelloController@index'); // 追加

調べていくと、どうやら app/Providers/RouteServiceProvider.php に原因がありそうでした。

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * 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'; // ← ここ

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

$namespace がコメントアウトされています。

ここのコメントアウトを解除したら、無事レスポンスが返ってくるようになりました。

$ curl https://tool.apprythm.com/api/index
Hello!

タイトルとURLをコピーしました