Target Class Controller Does Not Exist - Laravel 8

Error “Target class controller does not exist” when using Laravel 8

You are using Laravel 8. In a fresh install of Laravel 8, there is no namespace prefix being applied to your route groups that your routes are loaded into.

"In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property's value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel." Laravel 8.x Docs - Release Notes

You would have to use the Fully Qualified Class Name for your Controllers when referring to them in your routes when not using the namespace prefixing.

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);
// or
Route::get('/users', 'App\Http\Controllers\UserController@index');

If you prefer the old way:

App\Providers\RouteServiceProvider:

public function boot()
{
...

Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers') // <---------
->group(base_path('routes/api.php'));

...
}

Do this for any route groups you want a declared namespace for.

The $namespace property:

Though there is a mention of a $namespace property to be set on your RouteServiceProvider in the Release notes and commented in your RouteServiceProvider this does not have any effect on your routes. It is currently only for adding a namespace prefix for generating URLs to actions. So you can set this variable, but it by itself won't add these namespace prefixes, you would still have to make sure you would be using this variable when adding the namespace to the route groups.

This information is now in the Upgrade Guide

Laravel 8.x Docs - Upgrade Guide - Routing

With what the Upgrade Guide is showing the important part is that you are defining a namespace on your routes groups. Setting the $namespace variable by itself only helps in generating URLs to actions.

Again, and I can't stress this enough, the important part is setting the namespace for the route groups, which they just happen to be doing by referencing the member variable $namespace directly in the example.

Update:

If you have installed a fresh copy of Laravel 8 since version 8.0.2 of laravel/laravel you can uncomment the protected $namespace member variable in the RouteServiceProvider to go back to the old way, as the route groups are setup to use this member variable for the namespace for the groups.

// protected $namespace = 'App\\Http\\Controllers';

The only reason uncommenting that would add the namespace prefix to the Controllers assigned to the routes is because the route groups are setup to use this variable as the namespace:

...
->namespace($this->namespace)
...

Target class [UserController] does not exist Laravel 8

You are having error because you are defining controller namespace on each route in your web.php file (Such as 'App\Http\Controllers\UserController@profil') as well as setting value for protected $namespace variable inside your RouteServiceProvider.php (Such as protected $namespace = 'App\Http\Controllers';).

Now what you need to do is either define controller namespace on each route in web.php file or provide value for protected $namespace variable inside your RouteServiceProvider.php

You can't do both things at same time. Just choose what suites best to your situation.
Either remove the protected $namespace = 'App\Http\Controller'; from RouteServiceProvider.php file or remove controller namespaces that you have attached with each route in your web.php file. (Change Route::get('user/profil', 'App\Http\Controllers\UserController@profil')->name('user.profil'); to Route::get('user/profil', 'UserController@profil')->name('user.profil');) and everything should work fine.

For Laravel docs reference you can read https://laravel.com/docs/8.x/upgrade#routing

However many people would get this error because they are used to define their routes like
Route::get('/users','UserController@index); and it was fine to define it this way in Laravel versions till Laravel 7. But Laravel 8 has changed this method and rather provides new way of defining Routes.

  • Using PHP callable syntax which is Route::get('/users', [UserController::class, 'index']); With this way you will need to import the controller file into the web.php

    OR

  • Using string syntax, which is Route::get('/users', 'App\Http\Controllers\UserController@index');

Target class controller does not exist Laravel 8 in localhost everthing working fine but not online

You need to pay attention to the casing in your namespaces and class names.

In your web routes file, you have this line:

use App\http\Controllers\BookingController;

But in your actual controller, Http starts with a capital letter. So I’m guessing you’re developing on an OS that is case-insensitive (Windows) and deploying to something that is case-sensitive (something *nix based).

Target class does not exist. problem in laravel 8

Laravel 8 Update the way to write routes

ref link https://laravel.com/docs/8.x/upgrade

in laravel 8 you need to use like

use App\Http\Controllers\SayhelloController;
Route::get('/users/{name?}' , [SayhelloController::class,'index']);

or

Route::get('/users', 'App\Http\Controllers\UserController@index');

If you want to use old way

then in RouteServiceProvider.php

add this line

 /**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers'; // need to add in Laravel 8


public function boot()
{
$this->configureRateLimiting();

$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) // need to add in Laravel 8
->group(base_path('routes/api.php'));

Route::middleware('web')
->namespace($this->namespace) // need to add in Laravel 8
->group(base_path('routes/web.php'));
});
}

Then you can use like

Route::get('/users/{name?}' , [SayhelloController::class,'index']);
Route::resource('/users' , SayhelloController::class);

or

Route::get('/users', 'UserController@index');

Target class controller does not exist - laravel 7

"message": Target class controller
[App\\Http\\Controllers\\UploadController] does not exist.",

It appears Laravel is failing to find your Controller. Try clearing complied files and let composer try to auto-discover relevant classes.

In your terminal run these two commands one after the other.

php artisan clear-compiled 
composer dump-autoload

Laravel 5.8. Target class [Controller] does not exist

=> Open App\Providers\RouteServiceProvider.php and uncomment this line

protected $namespace = 'App\\Http\\Controllers';

Target class controller [controllerName] not found in Laravel 8

In the official Laravel 8 upgrade guide, you can see that controllers are no longer namespaced by default. This hinders the automatic class resolution with 'Controller@method' syntax.

See https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing

The new way of registering routes is to use the following syntax:

// routes/api.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\RegisterController;

Route::get('register', [RegisterController::class, 'register');

You can also adapt your RouteServiceProvider.php file to "re-enable" the old way to auto-load your controllers using the correct namespace with @ syntax:

// App/Providers/RouteServiceProvider.php

public function boot()
{
// ...

Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers') // put your namespace here
->group(base_path('routes/api.php'));

// ...
}


Related Topics



Leave a reply



Submit