javediq_143's avatar

Users Controller does not exist

Hello All,

I'm building a CMS in Laravel 5.8 with an in-built Authentication system. My app was working fine but suddenly it starts giving me an error as ReflectionException : Class App\Http\Controllers\Users does not exist on command php artisan route:list -v. However, if i define a new resource route, then that new route page works fine. It means, route file is getting saved but gives error while listing the routes.

I have not created any file named as Users but my user controller file name is UserController

Below is the structure of my application:

cms -> project folder
  app
    Http
      Controllers
        Auth
          RegisterController.php
          LoginController.php and other Auth files
        backend
          UserController.php and my other controller files

Below is my route file

Auth::routes();

Route::group(['as'=>'cms.'  ,'prefix'=>'cms'],function(){
    Route::get('/', 'backend\Dashboard@index')->name('dashboard');
    Route::resource('/user-management', 'backend\UserController');
});

Below is my UserController file residing in backend folder

<?php
namespace App\Http\Controllers\backend;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Auth;
use App\Models\backend\Admin;
use App\Models\backend\Menu;
use App\Models\backend\Submenu;
use Config;
use illuminate\Support\Facades\Validator;

class UserController extends Controller
{
   // other codes
}

I have tried the below commands and they all run successfully but none resolved the error

php artisan clear-compiled
composer dump-autoload 
php artisan optimize 
php artisan route:cache

Don't know from where this Users controller class in being referenced.

Can anyone help me in this regard as I'm badly stuck in my development.

Much Regards,

Javed

0 likes
7 replies
hiajayy's avatar

In your App\Providers is there RouteServiceProvider available?

javediq_143's avatar

@hiajayy yes there is such a file and below is its complete code. I updated the $namespace value from App\Http\Controllers to 'App\Http\Controllers\backend but still same error. Is there anything else i need to do after updating the $namespace value?

<?php

namespace App\Providers;

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

class RouteServiceProvider extends ServiceProvider
{
    /**
     * 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\backend';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        /*$router = app()->make('router');
        $paths = array();
        $paths = ['/'=>'backend\Dashboard@index', '/user-management'=>'backend\Users', '/menu-parent'=>'backend\Menumanagement', '/menu-child'=>'backend\Submenumanagement', '/privilege-management'=>'backend\Privileges'];
        foreach($paths as $index=>$path)
        {
            $router->resource('cms.'.$index, $path);
        }*/

        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(base_path('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(base_path('routes/api.php'));
    }
}
SarwarAhmed's avatar

Change on router

Route::resource('/user-management', 'App\Http\Controllers\backend\UserController');
javediq_143's avatar

A quick update. I changed my controller name from UserController to Users and run composer dump-autoload command. Still giving the same error on running php artisan route:list -v command. I feel laravel is looking for a Users controller in App\Http\Controller path. Why its not taking Users file from the backend folder inside the Controller folder? Where do i need to mention laravel to look for this file inside backend folder

javediq_143's avatar

@sarwarahmed the page at user-management URL is working fine. It means the route Route::resource('/user-management', 'backend\UserController'); is also correct.

javediq_143's avatar
javediq_143
OP
Best Answer
Level 1

Guys.. the error is resolved now. I searched for Users word in my entire project and found In api.php file there is this line Route::middleware('auth:api')->get('/user', 'Users@AuthRouteAPI');. So I made the changes in api.php to Route::middleware('auth:api')->get('/user', 'backend\Users@AuthRouteAPI'); and now my all my routes are listed. Thank u every one so much for ur effort.

Please or to participate in this conversation.