Latest

Thursday, July 13, 2017

Class not found after auto loading in Laravel

Asked by: Tower


I am struggling with PHP Namespaces and Auto loading in Laravel 5.4. I actually decided to put the models that I need to use in a definite and named folder as app/Models:

php artisan make:model Models/Customer

I have set Customer model's namespace to namespace Models;
Then, I created a route as follows:

Route::get('customer', function () {
    $customer = \App\Models\Cutomer::find(1);
    echo $customer;
});


To do auto loading work I opened the composer.json file located in the very root folder of the Laravel project and made the autoload to be as follows:

"autoload": {
    "classmap": [
        "database",
        "app/Models"
    ],
    "psr-4": {
        "App\\": "app/",
        "App\\Models\\": "App/Models"
    }
},

After all of this I checked if my composer is an update version and ran dump-autoload:

composer self-update
composer dump-autoload -o

There is also worth of notice the contents of vendor/composer/autoload_classmap.php:

'App\\Models\\Customer' => $baseDir . '/app/Models/Customer.php',
'App\\Models\\Test\\Test' => $baseDir . '/app/Models/Test.php',

My problem is that whenever I execute the url: http://127.0.0.1:8000/customer I will encounter the following error output:

(1/1) FatalThrowableError
Class 'App\Models\Cutomer' not found

Would you please help me understand where of my work has been incorrect, and how to fix it? Thank you very much in advance.



Source

No comments:

Post a Comment

Adbox