Skip to content
PHP and Laravel3 min read

Fix "Target class does not exist" in Laravel

Target class [HomeController] does not exist.

Short answer

Laravel throws this when it is asked to build a class (a controller, middleware or service) that the autoloader can't find. The usual causes are the old 'HomeController@index' route syntax in Laravel 8 and later, or a filename whose letter case doesn't match the class name on a Linux server.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Causes
  2. How to fix it
  3. Prevention
  4. On ArabyHost

The full message usually looks like this:

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

Laravel’s service container tried to instantiate the class named in the brackets, asked Composer’s autoloader for it, and no file matched. That bracketed name is your best clue: read it character by character before changing anything.

Causes

  • Old route syntax: since Laravel 8, RouteServiceProvider no longer prefixes App\Http\Controllers automatically, so Route::get('/', 'HomeController@index') looks for a class literally called HomeController with no namespace.
  • Letter case: Linux treats HomeController.php and Homecontroller.php as different files. The project runs on Windows or macOS and breaks on the server.
  • Wrong namespace: the file sits in app/Http/Controllers/Admin/ but its namespace line doesn’t include Admin.
  • Stale autoload files: you added or moved a class without regenerating vendor/composer/autoload_*.php, or uploaded an old vendor folder.
  • Cached routes: bootstrap/cache/routes-v7.php still points to classes you renamed or deleted.
  • Unregistered middleware: Target class [role] does not exist means the middleware alias was never registered.
  • A hidden earlier crash: [view] or [config] in the brackets usually means the app failed before those core services loaded.

How to fix it

  1. Rewrite the route in routes/web.php with class syntax:
use App\Http\Controllers\HomeController;

Route::get('/', [HomeController::class, 'index']);
  1. Match folder, filename and namespace. app/Http/Controllers/Admin/UserController.php must start with:
namespace App\Http\Controllers\Admin;

class UserController extends Controller

If you only changed the case of a filename on Windows, use git mv so the rename actually reaches the repository and the server.

  1. Rebuild the autoloader and clear caches over SSH, from the project folder:
composer dump-autoload -o
php artisan optimize:clear

The second command clears the route, config and view caches together. Without SSH, delete routes-v7.php and config.php from bootstrap/cache/, run composer dump-autoload -o locally and upload the whole vendor folder.

  1. Register the middleware. In Laravel 11 and later this lives in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
    ]);
})

In older versions, add it to $middlewareAliases (or $routeMiddleware) in app/Http/Kernel.php.

  1. Read the log at storage/logs/laravel.log when the bracketed name is a core service such as view, config or db. An earlier error, such as a missing .env or a broken config file, is the real cause, and fixing it removes this message.

Prevention

  • Always reference controllers as [Controller::class, 'method']; your editor flags missing classes immediately.
  • Keep filenames and class names identical, following PSR-4.
  • Make deployment repeatable: composer install --no-dev -o, then php artisan optimize, as in the Laravel deployment guide.
  • Test on Linux before going live when you can.

On ArabyHost

AI/Developer plans include SSH jailed in CageFS, with Composer and PHP 8.1 to 8.3, so you can run the composer and artisan commands above directly. You can also deploy from GitHub through cPanel Git Version Control and a .cpanel.yml file that runs the same build steps on every push; see the Git deployment guide. If the cause isn’t obvious, ask the Araby AI assistant in the panel: it reads your error log and explains the fix. Plan details are on the developers page and Laravel hosting.

FAQ

Why does it work on my machine but fail on the server?

Almost always letter case. Windows and macOS ignore case in filenames by default; Linux doesn't. Make the filename match the class name exactly, then run composer dump-autoload.

Can I bring back the old 'HomeController@index' syntax?

In Laravel 8 to 10 you can restore the namespace property in RouteServiceProvider. It's better to switch to [HomeController::class, 'index'], which is clearer and lets your editor catch mistakes early.

What does Target class [view] does not exist mean?

The app crashed before the view service was registered, so the real problem is elsewhere. Open storage/logs/laravel.log and look for the first error; it's often a missing .env file or a mistake in a config file.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.