0
votes

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
{
    public function __construct()
    {
        $this->middleware('auth:admin');
    }   

    public function AuthRouteAPI(Request $request)
    {
        return $request->user();
    }  

    public function index()
    {
        // GET THE CURRENT LOGGED IN USE
        $user = Auth::user();

        // CODE FOR GETTING MENU LIST FOR CURRENT USER STARTS
        $menutable = (new Menu)->getTable();
        $arrUserMenu = $user->getUserMenus($menutable);   
        // CODE FOR GETTING MENU LIST FOR CURRENT USER ENDS

        $cms_users = Admin::withTrashed()->get();

        return view('backend.pages.users.index', ['arrUserMenu'=>$arrUserMenu, 'cms_users'=>$cms_users]);
    }
    
    public function create()
    {
        // GET THE CURRENT LOGGED IN USE
        $user = Auth::user();

        // CODE FOR GETTING MENU LIST FOR CURRENT USER STARTS
        $menutable = (new Menu)->getTable();
        $arrUserMenu = $user->getUserMenus($menutable);   
        // CODE FOR GETTING MENU LIST FOR CURRENT USER ENDS

        return view('backend.pages.users.create', ['arrUserMenu'=>$arrUserMenu]);
    }
    
    public function store(Request $request)
    {
        $rules = [
            'name' => 'required|min:'.Config::get('cms_const.USER_NAME_MIN_LEN').'|max:'.Config::get('cms_const.USER_NAME_MAX_LEN').'|regex:/(^[A-Za-z ]+$)+/',
            'usrmail' => 'required|email|unique:cms_users,email|max:'.Config::get('cms_const.USER_EMAIL_MAX_LEN'),
            'usrname' => 'required|min:'.Config::get('cms_const.USER_ID_MIN_LEN').'|max:'.Config::get('cms_const.USER_ID_MAX_LEN').'|regex:/(^[A-Za-z0-9._]+$)+/|unique:cms_users,username',
            'usrpwd' => 'required|min:'.Config::get('cms_const.USER_PWD_MIN_LEN').'|max:'.Config::get('cms_const.USER_PWD_MAX_LEN').'|regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-_]).{7,}$/'
        ];

        $msgs = [
            'name.required' => 'Please enter name',
            'name.min' => 'Name should not be less then '.Config::get('cms_const.USER_NAME_MIN_LEN').' characters',
            'name.max' => 'Name should not be more then '.Config::get('cms_const.USER_NAME_MAX_LEN').' characters',
            'name.regex' => 'Only alphabets are allowed in name',

            'usrmail.required' => 'Please enter email',
            'usrmail.email' => 'Invalid email',
            'usrmail.unique' => 'Email already exist',
            'usrmail.max' => 'Email should not be more then '.Config::get('cms_const.USER_EMAIL_MAX_LEN').' characters',

            'usrname.required' => 'Please enter the user name',
            'usrname.min' => 'User name should not be less then '.Config::get('cms_const.USER_ID_MIN_LEN').' characters',
            'usrname.max' => 'User name should not be more then '.Config::get('cms_const.USER_ID_MAX_LEN').' characters',
            'usrname.regex' => 'Only Alphabets, Numbers, Dot and Underscore allowed in user name',
            'usrname.unique' => 'User name already exist',

            'usrpwd.required' => 'Please enter the password',
            'usrpwd.min' => 'Password should not be less then '.Config::get('cms_const.USER_PWD_MIN_LEN').' characters',
            'usrpwd.max' => 'Password should not be more then '.Config::get('cms_const.USER_PWD_MAX_LEN').' characters',
            'usrpwd.regex' => 'Invalid password format',
        ];

        $this->validate($request, $rules, $msgs);

        Admin::create([
            'name' => $request->name,
            'username' => $request->usrname,
            'password' => bcrypt($request->usrpwd),
            'email' => $request->usrmail,
        ]);

        return redirect()->route('cms.user-management.index')->with('success','New user account created successfully.');
    }
    
    public function edit($id)
    {
        if(!$id)
        {
            return redirect()->route('cms.user-management.index');
            die;
        }

        $arrRecord = Admin::find($id);
        if(is_null($arrRecord))
        {
            return redirect()->route('cms.user-management.index')->withErrors(['error'=>'Record you are trying to edit does not exist']);
            die;
        }

        // GET THE CURRENT LOGGED IN USE
        $user = Auth::user();

        // CODE FOR GETTING MENU LIST FOR CURRENT USER STARTS
        $menutable = (new Menu)->getTable();
        $arrUserMenu = $user->getUserMenus($menutable);   
        // CODE FOR GETTING MENU LIST FOR CURRENT USER ENDS

        return view('backend.pages.users.edit', ['arrUserMenu'=>$arrUserMenu, 'arrRecord'=>$arrRecord]);
    }

    public function update(Request $request, $id)
    {
        $rules = [
            'name' => 'required|min:'.Config::get('cms_const.USER_NAME_MIN_LEN').'|max:'.Config::get('cms_const.USER_NAME_MAX_LEN').'|regex:/(^[A-Za-z ]+$)+/',
            'usrmail' => 'required|email|max:'.Config::get('cms_const.USER_EMAIL_MAX_LEN'),
            'usrpwd' => 'nullable|min:'.Config::get('cms_const.USER_PWD_MIN_LEN').'|max:'.Config::get('cms_const.USER_PWD_MAX_LEN').'|regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-_]).{7,}$/'
        ];        

        $msgs = [
            'name.required' => 'Please enter name',
            'name.min' => 'Name should not be less then '.Config::get('cms_const.USER_NAME_MIN_LEN').' characters',
            'name.max' => 'Name should not be more then '.Config::get('cms_const.USER_NAME_MAX_LEN').' characters',
            'name.regex' => 'Only alphabets are allowed in name',

            'usrmail.required' => 'Please enter email',
            'usrmail.email' => 'Invalid email',
            'usrmail.max' => 'Email should not be more then '.Config::get('cms_const.USER_EMAIL_MAX_LEN').' characters',

            'usrpwd.min' => 'Password should not be less then '.Config::get('cms_const.USER_PWD_MIN_LEN').' characters',
            'usrpwd.max' => 'Password should not be more then '.Config::get('cms_const.USER_PWD_MAX_LEN').' characters',
            'usrpwd.regex' => 'Invalid password format',
        ];

        $this->validate($request, $rules, $msgs);

        $arrRecord = Admin::find($id);
        if(is_null($arrRecord))
        {
            return redirect()->route('cms.user-management.index')->withErrors(['error'=>'Record you are trying to edit does not exist']);
            die;
        }

        $arrRecord->name =  $request->input('name');
        $arrRecord->email = $request->input('usrmail');
        if($request->input('usrpwd'))
        {
            $arrRecord->password = bcrypt($request->input('usrpwd'));
        }
        $arrRecord->save();

        return redirect()->route('cms.user-management.index')->with('success','User account details updated successfully.');
    }

    public function destroy($id)
    {
        try
        {
            $user = Admin::findOrFail($id);
            $user->delete();
            return response()->json(['success'=>'Record successfully deleted', 'status'=>'Suspended']);
        }
        // catch(Exception $e) catch any exception
        catch(ModelNotFoundException $e)
        {
            return response()->json(['error'=>'Record you are trying to delete does not exist']);
        }       
    }
}

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

1
can you provide the actual error message? sounds like you are referencing a class named Users in your Controllerlagbox
Probably a typo, according to your question the error says App\Http\Controllers\Users does not exist Users or User? You have UserController, not UsersControllersta
Please can you add all off the code for your UserController.Rwd
@Rwd I hv edited my question & added the complete source code of my UserController.Javed
Hey @ClémentBaconnier i think i found the culprit. In api.php file there is this line Route::middleware('auth:api')->get('/user', 'Users@AuthRouteAPI'); Should i change it to Route::middleware('auth:api')->get('/user', 'backend\Users@AuthRouteAPI'); or gives a complete path like this Route::middleware('auth:api')->get('/user', 'App\Http\Controllers\backend\Users@AuthRouteAPI');Javed

1 Answers

0
votes

@ClémentBaconnier the error is resolved now. I made the changes in api.php to Route::middleware('auth:api')->get('/user', 'backend\Users@AuthRouteAPI'); and now all my routes are listed. Thanks u soooo much for ur advice.