简体   繁体   中英

composer autoload psr-4 not working

I'm using psr-4 to autoload and create a namespace. But I'm getting this error:

Fatal error: Class 'Models\User' not found in /app/routes/users.php on line 7

My architecture

app
---routes
------users.php
---VDB
------Models
---------User.php

在此处输入图片说明

This is my composer.json

{
    "autoload": {
        "psr-4": {
            "VDB\\": "app/VDB"
        }
    },
    "require": {
        "slim/slim": "~2.0",
        "slim/views": "^0.1.3",
        "twig/twig": "^1.23",
        "illuminate/database": "~5.0"
    }
}

My User.php

<?php

namespace VDB\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Eloquent {

}

My Route

<?php

use \Models\User;

$app->get('/users/:username', function($username) use ($app) {

    $user = User::where('username', $username)->first();

    var_dump($user);

})->name('users');

I already tried composer dump-autoload but it didn't work. Anybody else who can think about something?

You need to fix your import:

use VDB\Models\User;

If the class still can't be found, try running

$ composer dump-autoload 

For reference, see https://getcomposer.org/doc/03-cli.md#dump-autoload .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM