11
votes

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

ReflectionException in Route.php line 280: Class App\Http\Controllers\App\Controllers\UserController 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', 'PagesController@selectRoute');

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

        Route::get('/users', 'UserController@index');
        Route::get('/user/add', 'UserController@getAdd');
        Route::post('/user/add', 'UserController@postAdd');
        Route::get('/user/edit/{id}', 'UserController@getEdit');
        Route::post('/user/edit/{id}', 'UserController@postEdit');
        Route::get('/user/delete/{id}', 'UserController@delete');
    });
});

My UserController;

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\User;
use App\UserTypes;

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('UserController@index');
    }

    public function getEdit($id){

    }

    public function postEdit($id){

    }

    public function delete($id){
        User::find(current(Hashids::decode($id)))->delete();
        return Redirect::action('UserController@index');
    }

}

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

8
Try to run composer dumpauto command.Alexey Mezenin
What is the path of your UserController? As you are using namespace App\Http\Controllers it has to be inside app/Http/Controllers directoryARIF MAHMUD RANA
Thanks both, composer dumpauto did not make any differences. Also the controller is within the app/Http/Controllers directoryKieran Headley
When I remove the User::create(); part the error dissapears, will it have something to do with this?Kieran Headley

8 Answers

30
votes

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

Previously it was:

Route::get('/', 'PagesController@index');

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 App\Http\Controllers\PagesController;
4
votes

Replace this code

Route::group(['middleware' => 'isAdmin'], function(){
    Route::get('/admin', 'AdminController@index');

    Route::get('/users', 'UserController@index');
    Route::get('/user/add', 'UserController@getAdd');
    Route::post('/user/add', 'UserController@postAdd');
    Route::get('/user/edit/{id}', 'UserController@getEdit');
    Route::post('/user/edit/{id}', 'UserController@postEdit');
    Route::get('/user/delete/{id}', 'UserController@delete');
});

with this

Route::group(['middleware' => 'isAdmin'], function(){
    Route::get('/admin', 'AdminController@index');
    Route::group(['namespace' => YOUR_NAMESPACE], function(){
        Route::get('/users', 'UserController@index');
        Route::get('/user/add', 'UserController@getAdd');
        Route::post('/user/add', 'UserController@postAdd');
        Route::get('/user/edit/{id}', 'UserController@getEdit');
        Route::post('/user/edit/{id}', 'UserController@postEdit');
        Route::get('/user/delete/{id}', 'UserController@delete');
    });
});

& 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 App\Controllers;

use App\Http\Requests;
use App\User;
use App\UserTypes;

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('UserController@index');
    }

    public function getEdit($id){

    }

    public function postEdit($id){

    }

    public function delete($id){
        User::find(current(Hashids::decode($id)))->delete();
        return Redirect::action('UserController@index');
    }

}

& your route will be like this

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

    // Admin Only //
    Route::group(['middleware' => 'isAdmin'], function(){
        Route::get('/admin', 'AdminController@index');
        Route::group(['namespace' => '\App\Controllers'], function(){
            Route::get('/users', 'UserController@index');
            Route::get('/user/add', 'UserController@getAdd');
            Route::post('/user/add', 'UserController@postAdd');
            Route::get('/user/edit/{id}', 'UserController@getEdit');
            Route::post('/user/edit/{id}', 'UserController@postEdit');
            Route::get('/user/delete/{id}', 'UserController@delete');
        });
    });
});
3
votes

The create method is missing the 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'],
]);
2
votes

use App\Http\Controllers\UserController;

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', 'UserController@index');

1
votes

this happen because you are missing Controller class which has extends for it and others reasons depended on your actions.

to solve this problem you need to use Controller class in your UserController.

use App\Http\Controllers\Controller;

or you can easily avoiding that be type on the console

php artisan make:controller UserController

that will solving the problem.

0
votes

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

use App\Http\Controllers\UserController;

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

Fixed.

0
votes
Route::get('/', 'api\AppController@appInfo');

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

0
votes

DOCS Laravel 8.x controllers#resource-controllers

On file routes/web.php

use App\Http\Controllers\UserController; and then Route::resource('user',UserController::class);