Sentry Answers>Laravel>

How do I fix "target class does not exist" in Laravel?

How do I fix "target class does not exist" in Laravel?

Richard C.

The ProblemJump To Solution

Suppose you have a route definition that was working in Laravel version 7 or older, such as:

Click to Copy
Route::get('hello', array('uses' => 'HelloWorldController@sayHello'));

You will find that it generates errors in Laravel 8 and above. You will receive a runtime error like the following:

Click to Copy
Illuminate \ Contracts \ Container \ BindingResolutionException Target class [SomethingController] does not exist.

The Solution

Assume you have a Laravel controller like the one below:

Click to Copy
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HelloWorldController extends Controller { public function sayHello() { return "Hello World"; } }

In your web.php routes file you can call this controller in any of the first four ways below, but the last two will not work:

Click to Copy
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\HelloWorldController; # 1 Route::get('hello', [App\Http\Controllers\HelloWorldController::class, 'sayHello']); # 2 Route::get('hello', 'App\Http\Controllers\HelloWorldController@sayHello'); # 3 Route::get('hello', [HelloWorldController::class,'sayHello']); # 4 Route::get('hello', array('uses' => 'App\Http\Controllers\HelloWorldController@sayHello')); # Error! Route::get('hello', array('uses' => 'HelloWorldController@sayHello')); Route::get('hello', array('uses' => 'HelloWorldController::class@sayHello'));

Routes 1 and 2 both use the full path of the controller. Route 1 uses the array syntax and 2 uses the string syntax to point to the controller.

Route 3 is the same as route 1, except that it gives the controller name without a path, relying on it already being imported at the start of the file in the line use App\Http\Controllers\HelloWorldController;. Without this import statement, you would have to use the full path of the controller or Laravel will throw an error.

Route 4 is a combination of routes 1 and 3: an array with a string path.

The final two routes will not work because they do not use the full controller path. This was acceptable in Laravel before version 8 but is no longer allowed. For more about this change, read the Laravel 8 upgrade guide.

Which Style of Route to Use?

Always use the style of route 1. Here you give the full name of the controller and its type. This is easy to refactor and automated tools can check for errors. The magic string style used in routes 2 and 4 should not be used and is difficult to refactor. These syntaxes are from older versions of Laravel.

  • SentryHow Sentry Can Improve Your Laravel Application
  • SentryLaravel Error and Performance Monitoring
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.