Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

esorone's avatar

Route not working Target class does not exist

Hello All,

I just have a small question. Laravel is behaving strange (in my opinion :-) ). And I don't now why.

I was struggling for more than a day, just to get a route working. I know Laravel change it logic but still I don't understand.

My question is: Why is /admins and /users working, and if I change these into /admin or /user is give me the "Target class [DemoController] does not exist"

It is just in fresh install of laravel, and these the only routes I have except the default welcome one.

I just want to understand, why the plural is working and the singular not.

Kr esorone

Route::get('/admins', 'App\Http\Controllers\DemoController@adminDemo');  => WORKS
Route::get('/admin', 'App\Http\Controllers\DemoController@adminDemo'); => NOT WORKING
Route::get('/users', 'App\Http\Controllers\DemoController@userDemo'); => WORKS
Route::get('/user', 'App\Http\Controllers\DemoController@userDemo'); => NOT WORKING
0 likes
9 replies
GeordieJackson's avatar

Have you tried php artisan route:clear after making the changes?

Also, composer dump-autoload is sometimes needed.

esorone's avatar

Hey GeordieJackson,

Yes I did, and I also did php artisan optimize, but still the same behaviour.

Kr

GeordieJackson's avatar
Level 18

I can't reproduce the behaviour. It works fine for me - Laravel 8 fresh install.

Do you have any namespace set in your RouteServiceProvider?

Have you tried the new tuple syntax?

e.g.

Route::get('/admin', [DemoController::class, 'adminDemo']);
Route::get('/admins', [DemoController::class, 'adminDemo']);
Route::get('/users', [DemoController::class, 'adminDemo']);
Route::get('/user',[DemoController::class, 'adminDemo']);

Remembering to import DemoController.

esorone's avatar

Hello Geordie, I did.. And the DemoContoller works with /admins and not with /admin. So the imput should be okay... What I noticed thought is that in my vendor directory I have multiple folders with the same name and a (2) behind it. I got the impression that something went wrong during the installation.

Thanks for looking into this topic

esorone's avatar

I just tried one more time with you solution and it worked.

In the end I used:

Route::get('/viewer', [DemoController::class, 'getViewerPage']);
Route::get('/manager', [DemoController::class, 'getManagerPage']);
Route::get('/user', [DemoController::class, 'getUserPage']);
Route::get('/admin', [DemoController::class, 'getAdminPage']);

Thanks. Now I got the solution, but I would really love to now what is missed :-)

tisuchi's avatar

@esorone What do you mean by NOT WORKING? Are you getting 404 or something else?

esorone's avatar

Hey I received a error message: Target class [DemoController] does not exist

siangboon's avatar

usually i see the "target class does not exist" for Controller issues are related to the typo error naming of the class or wrong namespace especially duplicate from other similar controller.

i think it may be better to show the EXACT code for all route list and also the DemoController...

esorone's avatar

Hello Siangboon,

Underneath the exact code. I updated it a little, because Im still working on it. But the User and Admin routes does not work, the viewer and manager does work

My routes file


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;

/*
|--------------------------------------------------------------------------
| 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('/viewer', 'App\Http\Controllers\DemoController@getViewerPage')->name('viewer');
Route::get('/user', 'App\Http\Controllers\DemoController@getUserPage')->name('user');
Route::get('/manager', 'App\Http\Controllers\DemoController@getManagerPage')->name('manager');
Route::get('/admin', 'App\Http\Controllers\DemoController@getAdminPage')->name('admin');


Route::get('/home', [\App\Http\Controllers\HomeController::class, 'index'])->name('home');

The demo controller:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    
    public function getViewerPage()
    {
        return view('viewer.index');
    }

    public function getUserPage()
    {
        return view('user.index');
    }

    public function getManagerPage()
    {
        return view('manager.index');
    }

    public function getAdminPage()
    {
        return view('admin.index');
    }


}

As mentioned, nothing fancy, but I just don't see the error. On User and Admin i receive "Target class [DemoController] does not exist."

But in the end I used the solution of GeorgieJackson which turned out to work. But I still don't now what the error was.

Please or to participate in this conversation.