. Advertisement .
..3..
. Advertisement .
..4..
The error “Target class does not exist” in Laravel 8 is a common error that can show up in many ways. In this blog, we will go through some of the ways you can fix this issue. Read on.
When Do You Get The Error: “Target class does not exist” in Laravel 8?
This can be your UserController.php.
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function login(Request $request)
{
print_r('login');
}
}
This can be your route api.php.
Route::get('login', 'Api\[email protected]');
Yet, you may encounter the following error.
Target class [Api\UserController] does not exist.
How To Solve The Error: “Target class does not exist” in Laravel 8?
Approach 1: Include the controller namespace in routes
Here is the official word from Laravel 8 about routes. The RouteServiceProvider has a $namespace property in prior Laravel editions. The value of this variable will be prefixed to controller route definitions or calls to the action helper / URL::action method by default. This property is null by default in Laravel 8.x. This indicates that Laravel will not prefix namespaces automatically.
When referring to any controllers in the routes without utilizing the namespace prefixing, utilize their fully qualified class names. You simply add your controller namespace in Laravel 8 in this way:
use App\Http\Controllers\UserController;
Route::get('/login', [UserController::class, 'login']);
Approach 2: Utilize namespace in this way
Simply set the whole path in the route to something like this:
Route::get('/login', 'App\Http\Controllers\[email protected]');
Approach 3: Delete the comment in RouteServiceProvider.php
To begin with, go to app\Providers\RouteServiceProvider.php and access RouteServiceProvider.php.
Delete this line’s comment now.
protected $namespace = 'App\\Http\\Controllers';
Approach 4: Utilize the Action Syntax
Excepting the solutions mentioned above, there is another solution for you to solve the error “Target class does not exist” in Laravel 8. It is utilizing the Action Syntax.
This is the solution we are directly instructing the code which class to use, we believe this solution to be more error-proof and to offer greater IDE help. You can provide the class and method to utilize in an array using the action syntax rather than your standard string syntax:
// String syntax
Route::get('/posts', '[email protected]');
// Action syntax (remember to import your controller class)
Route::get('/posts', [PostsController::class, 'all']);
As you can see, you are supplying PostsController::class here rather than the PostsController itself, which will internally return ‘AppHttpControllersPostsController’. The method to call within that controller is indicated by the second element in the array, which reads as follows: “In PostsController.php, call the ‘all’ method.
Once more, if you try to start your program, everything will be functional.
Conclusion
We hope you enjoyed our article about the error. With this knowledge, we know that you can fix your error “Target class does not exist” in Laravel 8 quickly by following these steps! If you still have any other questions about fixing this syntax error, please leave a comment below. Thank you for reading!
Read more
→ Tips On Solving The Error “Target class [ApiUserController] does not exist in laravel 8”
Leave a comment