SoWizardly SoWizardly - 7 months ago 59
PHP Question

Laravel 5.1 "ReflectionException in Container.php line 737:..class does not exist"

I'm sure that the solution to this problem is staring me right in the face, but unfortunately I can't seem to figure it out. I'm trying to add a route to my laravel 5.1 installation, and I'm getting the below error...

ReflectionException in Container.php line 737:
Class App\Http\Controllers\Tools\DashBoardController does not exist


I first edited the routes file to include the below...

(file: app\Http\routes.php)

Route::get('dashboard', 'Tools\DashBoard@index');


Then I created the "Tools" folder and "DashBoardController.php" file, and have it setup to look somewhat like the below...
(file: app\Http\Controllers\Tools\DashBoardController.php)

namespace App\Http\Controllers\Tools\DashBoard;
//...etc...//
class DashBoardController extends Controller { /* ..etc.. */ }


Is this all I have to do? I read that you could run "composer dumpautoload" in the terminal but unfortunately that didn't help.

I'm on a localhost XAMPP install w/PHP7 on Win7, if that's useful. Any help is greatly appreciated!

Answer

Your namespace declaration should look like namespace App\Http\Controllers\Tools and should not contain the filename or name of your class. Then you need to change your route to point to the name of your class Route::get('dashboard', 'Tools\DashBoardController@index');.

The way autoloaders and namespaces work in PHP and specifically in Laravel is that the namespace must reflect the directory structure and the class name must match its filename.

If you will have multiple routes using controllers from the same namespace, you will probably benefit from implementing route group namespaces.