简体   繁体   中英

Class not found when using PSR-4 Autoload in Linux, works in Windows

Introduction

Hello, I moved my files from my local pc, running WAMP to my webserver, which is a Linux machine.
I work with composer to use its autoload functionality to load my MVC structure, more about that later.

The error that I receive on my webpage is the following:
Fatal error: Uncaught Error: Class 'App\\Model\\DB' not found in <folder_structure>/config/_boot.php:15

I do not have this error on my Windows machine, the code works perfectly there.

Folder structure

I use the same folder structure, which is (simplified) as following:

- config
-- _boot.php
- dist
-- index.php
-- includes
--- header.php
- src
-- app
--- Models
---- db.php
- composer.json

Code parts

My config/_boot.php file looks like this:

use App\Model\DB;
...
$db = new DB($database['host'], $database['dbname'], $database['user'], $database['password']);

My src/app/Model/db.php file looks like this:

namespace App\Model;
class DB
{
}

My composer.json contains this:

"autoload": {
        "psr-4": {
            "App\\": "src/app/"
        },
        "files": [
            "src/app/functions.inc.php",
            "config/_boot.php",
            "src/app/Routing.php"
        ]
    }

autoload_psr4.php

return array(
    ...
    'App\\' => array($baseDir . '/src/app'),
    ...
);

Question

Is there anyone who has an idea of what I am doing wrong?

Things I have tried / Checked

  • Check the folder permissions
  • I tried adding "App\\\\Model\\\\": "src/app/Model/" to my composer.json as well

PS: This is my first question on Stackoverflow, tips on improving the layout are welcome...

PSR-4 states:

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

You have broken this rule by placing DB class into db.php file. The reason it works on Windows and not on Linux is that the later is case-sensitive regarding file and folder names.

So the fix is to rename db.php into DB.php .

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