# Fix Target Class Does Not Exist Error in Laravel

> Resolve the Laravel BindingResolutionException by using fully qualified controller class names in route definitions, required since Laravel 8

**URL:** https://sentry.io/answers/how-do-i-fix-target-class-does-not-exist-in-laravel/

---

## The Problem

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

```php
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:

```
Illuminate \ Contracts \ Container \ BindingResolutionException
Target class [SomethingController] does not exist.
```

## The Solution

Assume you have a Laravel controller like the one below:

```php
<?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:

```php
<?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](https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing).

### 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.

---

*Source: [sentry.io/answers/how-do-i-fix-target-class-does-not-exist-in-laravel/](https://sentry.io/answers/how-do-i-fix-target-class-does-not-exist-in-laravel/)*
