Search code examples
laravellaravel-4composer-php

ReflectionException error on php artisan routes


Everytime I run php artisan routes, I get ReflectionException error and that UsersController class does not exist. I'm pretty sure it does.

I tried php composer dump-autoload and php artisan clear-compiled, they all give the same error.

This is my error log in /app/storage/logs/

[2014-03-30 01:41:24] production.ERROR: exception 'ReflectionException' with message 'Class UsersController does not exist' in C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Routing\ControllerInspector.php:28
Stack trace:
#0 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Routing\ControllerInspector.php(28): ReflectionClass->__construct('UsersController')
#1 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Routing\Router.php(269): Illuminate\Routing\ControllerInspector->getRoutable('UsersController', 'users')
#2 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Routing\Router->controller('users', 'UsersController')
#3 C:\wamp\www\re3\app\routes.php(14): Illuminate\Support\Facades\Facade::__callStatic('controller', Array)
#4 C:\wamp\www\re3\app\routes.php(14): Illuminate\Support\Facades\Route::controller('users', 'UsersController')
#5 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Foundation\start.php(269): require('C:\wamp\www\re3...')
#6 [internal function]: {closure}(Object(Illuminate\Foundation\Application))
#7 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(792): call_user_func(Object(Closure), Object(Illuminate\Foundation\Application))
#8 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(569): Illuminate\Foundation\Application->fireAppCallbacks(Array)
#9 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(552): Illuminate\Foundation\Application->bootApplication()
#10 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Console\Application.php(44): Illuminate\Foundation\Application->boot()
#11 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Console\Application.php(33): Illuminate\Console\Application::make(Object(Illuminate\Foundation\Application))
#12 C:\wamp\www\re3\artisan(46): Illuminate\Console\Application::start(Object(Illuminate\Foundation\Application))
#13 {main} [] []

Solution

  • Somehow Laravel is not being able to find your UsersController, run

    composer dumpautoload
    

    Then check the file vendor/composer/autoload_classmap.php, your UsersController has to be there, otherwise composer won't be able to autoload it and Laravel will not have access to it.

    if you cannot find your controllers in int, you have to check:

    1) Your composer.json file, the folder where your controllers are must be in:

    "autoload": {
        "classmap": [
            "app/controllers",
                      ....
        ],
    

    2) Check if your classes are correctly named.

    3) If you are using a namespace:

    class UsersController extends Controller { ... }

    You must use the namespace in your references to it and, probably it would be better, in this case, to use PSR-4 (or even PSR-0) to autoload your classes.

    4) Compare classes that you have in autoload_classmap.php with those that are not there. There must be a difference in naming or directory placement.

    5) Check if your classes all start with

    <?php 
    

    and not just

    <?
    

    This may not make too much difference for composer nor PHP, but it does for Laravel.