close

[Solved] Target class [Api\UserController] does not exist in laravel 8

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing following error Target class [Api\UserController] does not exist in laravel 8 in Laravel. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Target class [Api\UserController] does not exist in laravel 8 Error Occurs ?

Here is my 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');
    }
}

Here is my route api.php

Route::get('login', 'Api\[email protected]');

But I am facing this error.

Target class [Api\UserController] does not exist.

How To Solve Target class [Api\UserController] does not exist in laravel 8 Error ?

  1. How To Solve Target class [Api\UserController] does not exist in laravel 8 Error ?

    To Solve Target class [Api\UserController] does not exist in laravel 8 Error Here we have to use Fully Qualified Class Name for your Controllers when referring to them in your routes when not using the namespace prefixing. In Laravel 8 you just add your controller namespace. Second solution is Just assign full path in route something like this. Route::get('/login', 'App\Http\Controllers\[email protected]');

  2. Target class [Api\UserController] does not exist in laravel 8

    To Solve Target class [Api\UserController] does not exist in laravel 8 Error Here we have to use Fully Qualified Class Name for your Controllers when referring to them in your routes when not using the namespace prefixing. In Laravel 8 you just add your controller namespace. Second solution is Just assign full path in route something like this. Route::get('/login', 'App\Http\Controllers\[email protected]');

Solution 1: add your controller namespace in routes

Here is laravel 8 official message related to routes 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.

Here we have to use Fully Qualified Class Name for your Controllers when referring to them in your routes when not using the namespace prefixing. In Laravel 8 you just add your controller namespace Something Like this.

use App\Http\Controllers\UserController;

Route::get('/login', [UserController::class, 'login']);

Solution 2: Use namespace like this

Just assign full path in route something like this.

Route::get('/login', 'App\Http\Controllers\[email protected]');

Solution 3: remove the comment from RouteServiceProvider.php

First of all Open RouteServiceProvider.php located at app\Providers\RouteServiceProvider.php

Now remove comment of following line.

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

Summary

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Please Rate Our Solution to Motivate Us For Give Our Best

Leave a Comment