lundi 2 novembre 2015

Laravel 5 trouble autoloading class - Class Not Found Error

I am attempting to autoload my classes. I create a folder app/Classes with a file Foo.php and a file Bar.php.

In composer.json I have the folder referenced

"autoload": {
    "classmap": [
        "database",
        "app/Classes/"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": ["app/Library/myFunctions.php"]
},

Follow this by

composer dump-autoload

Verified the vandor/autoload/autoload_classmap.php has both

'Bar' => $baseDir . '/app/Classes/Bar.php',
'Foo' => $baseDir . '/app/Classes/Foo.php',

I put a simple function in each class like:

<?php 
    class Bar {
        public function __construct(){
            echo 'Bar';
        }
    }
?>

Then I attempt to reference it

new Bar;

and receive the error

FatalErrorException in LoginController.php line 24:
Class 'App\Http\Controllers\Login\Bar' not found

NOW..... to solve the error I put

new \Bar;

and we are good.

Is there some reason I need the '\' in my code. This fix is nowhere for me to find in docs, I just noticed it on laracasts and added it in frustration, and it works.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire