Target class does not exist when I’m trying to acces API route

  Kiến thức lập trình

When I’m trying to access my route via http://localhost:8000/api/dnd/array, I get error:
Target class [Api/DndController] does not exist.
https://flareapp.io/share/87ndNRBm

I’ve use these commands to make api controller
php artisan install:api
php artisan make:controller Api/DndController

Then I’ve make function in controller to handle request and give response.

app/Http/Controllers/Api/DndController.php

<?php

namespace AppHttpControllersApi;

use AppHttpControllersController;
use IlluminateHttpRequest;

class DndController extends Controller
{
    public function getData(Request $request, $type) {
         $classes = [
            'Barbarian' => 'A fierce warrior of primitive background who can enter a battle rage',
            'Bard' => 'An inspiring magician whose power echoes the music of creation',
            'Cleric' => 'A priestly champion who wields divine magic in service of a higher power',
            'Druid' => 'A priest of the Old Faith, wielding the powers of nature—moonlight and plant growth, fire and lightning—and adopting animal forms',
            'Fighter' => 'A master of martial combat, skilled with a variety of weapons and armor',
        ];
        $allowedTypes = ['array', 'json', 'collection'];
        if (!in_array($type, $allowedTypes)) {
            return response()->json(['error' => 'Invalid type'], 400);
        } else {
            if ($type === 'array') {
                return $classes;
            } elseif ($type === 'json') {
                return response()->json($classes);
            } else {
                return collect($classes);
            }
        } 

    }
}

And I’ve also add this controller as route in api.php

routes/api.php

<?php

use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;

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

Route::get('dnd/{type}', 'Api/DndController@getData');

I was trying to change controller target class to:
Route::get('dnd/{type}', 'DndController@getData');, Route::get('dnd/{type}', 'ApiDndController@getData'); , Route::get('dnd/{type}', 'appHttpControllersApiDndController@getData'); , Route::get('dnd/{type}', 'app/Http/Controllers/Api/DndController@getData'); , Route::get('dnd/{type}', 'DndController@getData');
But it doesn’t help.
I’m struggling to grasp Laravel, and I’m hoping for some guidance. Your assistance would be greatly appreciated.

New contributor

Nikita Skliarov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT