mozew's avatar
Level 6

Target class [Modules\User\Http\Controllers\Modules\User\Http\Controllers\UserController] does not exist.

I installed modules when I create a User module I write in web.php

use Illuminate\Support\Facades\Route;
use Modules\User\Http\Controllers\UserController;

Route::resource('users', UserController::class);

I see this error

Target class [Modules\User\Http\Controllers\Modules\User\Http\Controllers\UserController] does not exist.

0 likes
26 replies
Sinnbeck's avatar

Are you sure that's the file giving the error?

Sinnbeck's avatar

@irankhosravi the code shown should work. So I am asking if the error might be coming from another file. Like inside UserController perhaps?

mozew's avatar
Level 6

@Sinnbeck

<?php

namespace Modules\User\Http\Controllers;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\User\Entities\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        $users = User::query();
        
        if($keyword = request('search')) {
            $users->where('first_name', 'LIKE', "%{$keyword}%")
                 ->orWhere('last_name', 'LIKE', "%{$keyword}%");
        }

        if(request('admin')) {
            $users->where('level' , 'admin');
        }

        $users = $users->latest()->paginate(25);
        return view('user::admin.index', compact('users'));

    }
Sinnbeck's avatar

@irankhosravi just remove the protected $namespace as already suggested and the error will be fixed

1 like
Sinnbeck's avatar

Do you have namespace set it in the RouteServiceProvider? If so remove it

tykus's avatar

Do you have a default namespace defined in the RouteServiceProvider, e.g.

// app/Providers/RouteServiceProvider.php
protected $namespace = '\Modules\User\Http\Controllers';

mozew's avatar
Level 6

@tykus

<?php

namespace Modules\User\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The module namespace to assume when generating URLs to actions.
     *
     * @var string
     */
    protected $namespace = 'Modules\User\Http\Controllers';

    /**
     * Called before routes are registered.
     *
     * Register any model bindings or pattern based filters.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(module_path('User', '/Routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(module_path('User', '/Routes/api.php'));
    }
}
tykus's avatar

Remove this:

protected $namespace = 'Modules\User\Http\Controllers';

(unless every Controller is under that namespace in your application)

What is this Modules package anyway - something third-party or your own???

tykus's avatar
tykus
Best Answer
Level 104

@irankhosravi I mean delete the line protected $namespace = 'Modules\User\Http\Controllers'; from the RouteServiceProvider class.

1 like
limewater23's avatar
restart the server
php artisan cache:clear; php artisan optimize 
kokoshneta's avatar

@limewater23 Why are you resurrecting an old thread that already has the correct solution, only to suggest something that wouldn’t do any good at all?

limewater23's avatar

@kokoshneta it's a supplement, I tried your correct solution, but it didn't work for me, and it took a while and I figured out and typed here if only people have a similar experience.

amir tavakolian's avatar

just to like this

​ Route::group(['namespace'=>'Tavakolian\User\Http\Controllers'], function(){ Auth::routes(); Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); }); ​

change the namespace with your own namespace i mean change the 'Tavakolian\User\Http\Controllers' with your own

Please or to participate in this conversation.