[Solved] Class AppHttpControllersUserController Does Not Exist

Having the issue when loading the route /users or /user/add and being return an error of;

ReflectionException in Route.php line 280: Class
AppHttpControllersAppControllersUserController does not exist

The UserController does exist and it is not in a folder within my controllers folder.

My route file;

Route::group(['middleware' => 'auth'], function(){
    Route::get('/route/selector', '[email protected]');

    // Admin Only //
    Route::group(['middleware' => 'isAdmin'], function(){
        Route::get('/admin', '[email protected]');

        Route::get('/users', '[email protected]');
        Route::get('/user/add', '[email protected]');
        Route::post('/user/add', '[email protected]');
        Route::get('/user/edit/{id}', '[email protected]');
        Route::post('/user/edit/{id}', '[email protected]');
        Route::get('/user/delete/{id}', '[email protected]');
    });
});

My UserController;

<?php

namespace AppHttpControllers;

use AppHttpRequests;
use AppUser;
use AppUserTypes;

use Auth;
use Hashids;
use Redirect;
use Request;
use Hash;

class UserController extends Controller
{
    public function index(){
        $users = User::get();
        return view('users.index', compact('users'));
    }

    public function getAdd(){
        $user_type = UserTypes::pluck('user_type', 'id');
        return view('users.add', compact('user_type'));
    }

    public function postAdd(){
        $input = Request::all();
        $password = str_random(8);
        User::create(
            'email' => $input['email'],
            'password' => Hash::make($password),
            'first_name' => $input['first_name'],
            'surname' => $input['surname'],
            'phone_number' => $input['phone_number'],
            'user_type' => $input['user_type'],
        );

        return Redirect::action('[email protected]');
    }

    public function getEdit($id){

    }

    public function postEdit($id){

    }

    public function delete($id){
        User::find(current(Hashids::decode($id)))->delete();
        return Redirect::action('[email protected]');
    }

}

When I remove the User::create(); part the error disappears, will it have something to do with this?

Solution #1:

Laravel 8.x update has a different way of using routes.

Previously it was:

Route::get('/', '[email protected]');

Now it has changed to

Route::get('/',[PagesController::class, 'index']);

Note: don’t forget to import (use) your controller in the routes(web.php) file at the top. Like:

use AppHttpControllersPagesController;
Respondent: Kieran Headley

Solution #2:

Replace this code

Route::group(['middleware' => 'isAdmin'], function(){
    Route::get('/admin', '[email protected]');

    Route::get('/users', '[email protected]');
    Route::get('/user/add', '[email protected]');
    Route::post('/user/add', '[email protected]');
    Route::get('/user/edit/{id}', '[email protected]');
    Route::post('/user/edit/{id}', '[email protected]');
    Route::get('/user/delete/{id}', '[email protected]');
});

with this

Route::group(['middleware' => 'isAdmin'], function(){
    Route::get('/admin', '[email protected]');
    Route::group(['namespace' => YOUR_NAMESPACE], function(){
        Route::get('/users', '[email protected]');
        Route::get('/user/add', '[email protected]');
        Route::post('/user/add', '[email protected]');
        Route::get('/user/edit/{id}', '[email protected]');
        Route::post('/user/edit/{id}', '[email protected]');
        Route::get('/user/delete/{id}', '[email protected]');
    });
});

& in your UserController you should correct your namespace also

e.g your UserController resides in app/Controllers directory then your UserController will be like this

<?php

namespace AppControllers;

use AppHttpRequests;
use AppUser;
use AppUserTypes;

use Auth;
use Hashids;
use Redirect;
use Request;
use Hash;

class UserController extends Controller
{
    public function index(){
        $users = User::get();
        return view('users.index', compact('users'));
    }

    public function getAdd(){
        $user_type = UserTypes::pluck('user_type', 'id');
        return view('users.add', compact('user_type'));
    }

    public function postAdd(){
        $input = Request::all();
        $password = str_random(8);
        User::create(
            'email' => $input['email'],
            'password' => Hash::make($password),
            'first_name' => $input['first_name'],
            'surname' => $input['surname'],
            'phone_number' => $input['phone_number'],
            'user_type' => $input['user_type'],
        );

        return Redirect::action('[email protected]');
    }

    public function getEdit($id){

    }

    public function postEdit($id){

    }

    public function delete($id){
        User::find(current(Hashids::decode($id)))->delete();
        return Redirect::action('[email protected]');
    }

}

& your route will be like this

Route::group(['middleware' => 'auth'], function(){
    Route::get('/route/selector', '[email protected]');

    // Admin Only //
    Route::group(['middleware' => 'isAdmin'], function(){
        Route::get('/admin', '[email protected]');
        Route::group(['namespace' => 'AppControllers'], function(){
            Route::get('/users', '[email protected]');
            Route::get('/user/add', '[email protected]');
            Route::post('/user/add', '[email protected]');
            Route::get('/user/edit/{id}', '[email protected]');
            Route::post('/user/edit/{id}', '[email protected]');
            Route::get('/user/delete/{id}', '[email protected]');
        });
    });
});
Respondent: Maz341

Solution #3:

The create method was missing the correct array brackets.

User::create([
            'email' => $input['email'],
            'password' => Hash::make($password),
            'first_name' => $input['first_name'],
            'surname' => $input['surname'],
            'phone_number' => $input['phone_number'],
            'user_type' => $input['user_type'],
        ]);
Respondent: ARIF MAHMUD RANA

Solution #4:

use AppHttpControllersUserController;

Route::get(‘/user’, [UserController::class, ‘index]);

Laravel 8 has updated the route format. above route will only only for laravel 8 or higher

if you laravel below 8 try using

Route::get(‘/user’, ‘[email protected]‘);

Respondent: Kieran Headley

Solution #5:

Laravel 8 updated the Route format, please try the updated format on controllers routes.

use AppHttpControllersUserController;

Route::get(‘/user’, [UserController::class, ‘index’]);

Fixed.

Respondent: Siddy

Solution #6:

Route::get('/', 'api[email protected]');

I created AppController in controller/api folder so this is my path. You need to give path till your controller.

Respondent: Manu R S

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.