21

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?

5
  • 2
    Try to run composer dumpauto command. Commented May 19, 2016 at 11:25
  • 1
    What is the path of your UserController? As you are using namespace App\Http\Controllers it has to be inside app/Http/Controllers directory Commented May 19, 2016 at 11:49
  • Thanks both, composer dumpauto did not make any differences. Also the controller is within the app/Http/Controllers directory Commented May 19, 2016 at 11:56
  • When I remove the User::create(); part the error dissapears, will it have something to do with this? Commented May 19, 2016 at 12:13
  • you are missing the use of the class at the top . Commented Jan 15, 2022 at 22:25

13 Answers 13

55

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; 
Sign up to request clarification or add additional context in comments.

3 Comments

That happened to me when me did save movement controller file (under refactor option) to a folder. I did what @Maz341 said but additionally have corrected namespace in MyController class file, removed prefix(one's folder) in [prefix\MyController::class, '...'] and reimported Controller parent. And relaunched the server . Now its all ok
Thanks man... I came back from Laravel 5. Lot of things changed.
In my case, I was using lower case a in use App\Http\Controllers\PagesController;. Just convert in to upper case
5

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'); }); }); }); 

5 Comments

My User Controller resides in app/Http/Controllers not app/Controllers would this make a difference? I have built many laravel apps before and this has never been an issue.
When I remove the User::create(); part the error disappears, will it have something to do with this?
ReflectionException in Route.php line 280: Class App\Http\Controllers\App\Controllers\UserController does not exist
@KieranHeadley Your route namespace is incorrect. If I see your route service provide & route then I could have find the solution. For temporary you can use my solution of route part & replace this text Route::group(['namespace' => '\App\Controllers'], function(){ with this Route::group(['namespace' => '\App\Http\Controllers'], function(){
My route namespace is set to 'App\Http\Controllers' in /app/Providers/RouteServiceProvider.php
3

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'], ]); 

Comments

3
use App\Http\Controllers\UserController; Route::get('/user', [UserController::class, 'index']); 

Laravel 8 has updated the route format. The above route will only be for Laravel 8 or higher. If your Laravel version is less than 8 try using:

Route::get('/user', 'UserController@index'); 

Comments

2

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.

Comments

2

Don't forget to add this line to your controller

use App\Http\Controllers\Controller; 

If this is not present in your controller your controller is not able to extend the features of the main Laravel controller prototypes

Comments

2

be sure to type correctly use App\Http\Controllers\Controller; as my mistaken was with incorrect typing of use App\Http\Controller\Controller;

just instead of controllers/controller I mistakenly missed s at the end of controllers/controller and I typed controller/controller which was incorrect.

Comments

0

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.

Comments

0

Actually You are getting 2 errors, the last one gets shown on the top of the page, and that error is that the controller does not exist. The reason why you are seeing that error is because the Model::create() method expects an array of attributes, whilst you are calling it with separate arguments instead.

Try:

$user = 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'], ]); 

Comments

-1
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.

Comments

-1

DOCS Laravel 8.x controllers#resource-controllers

On file routes/web.php

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

Comments

-1

in laravel 8 do the following remove the comment from RouteServiceProvider.php First of all Open RouteServiceProvider.php located at app\Providers\RouteServiceProvider.php

Now remove comment of following line.

protected $namespace = 'App\Http\Controllers';

Comments

-1

The best solution for this problem is to open the file "Providers/RouteServiceProvider" and edit it. But, first, add it to the RouteServiceProvider class.

protected $namespace = 'App\Http\Controllers'; 

Next, in the boot() function.

$this->configureRateLimiting(); $this->routes(function () { Route::middleware('api') ->prefix('api') ->group(base_path('routes/api.php')); Route::namespace($this->namespace) ->middleware('web') ->group(base_path('routes/web.php')); Route::namespace($this->namespace) ->middleware('web') ->group(base_path('routes/dashboard.php')); }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.