Target class [UserController] does not exist
according to laravel doc
you can do it in two ways:
1- Using PHP callable syntax...
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
2- Using string syntax...
Route::get('/users', 'App\Http\Controllers\UserController@index');
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 likeRoute::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.phpOR
Using string syntax, which is
Route::get('/users', 'App\Http\Controllers\UserController@index');
Class App\Http\Controllers\API\UserController does not exist
If your controller path is /App/Http/Controllers/API
, you need to adjust it's namespace :
namespace App\Http\Controllers\API;
If your controller path is /App/Http/Controllers
, you need to adjust your routes:
Route::post('login', 'UserController@login');
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 theaction
helper /URL::action
method. In Laravel 8.x, this property isnull
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)
...
Class App\Http\Controllers\ does not exist
At the first of controller you do not need to put @
, its just for method of controller.
Route::get('/hello', 'HomeController@index');
Class App\Http\Controllers\ does not exist
At the first of controller you do not need to put @
, its just for method of controller.
Route::get('/hello', 'HomeController@index');
Related Topics
PHP String Concatenation - "$A $B" VS $A . " " . $B - Performance
Connect to Azure Active Directory from PHP Web Application
Http Auth via PHP - PHP_Auth_User Not Set
Why Don't PHP and JavaScript's Timestamps Match
Implementing "Update If Exists" in Doctrine Orm
How to Inject a Service in Another Service in Symfony
Model->Save() Not Working in Yii2
Symfony: Change Database Dynamically
Display Price on Add to Cart Button from the Functions.PHP File in Woocommerce
How to Select 10 Random Things from a List in PHP
What Is Laravel Render() Method For
Use of Undefined Constant Stdin - Assumed 'Stdin' in C:\Wamp\Www\Study\Sayhello.PHP on Line 5
Pdo's Rowcount() Not Working on PHP 5.2.6+
Why Ob_Start() Must Come Ahead of Session_Start() to Work in PHP
How to Send a File via Axios to Laravel