3

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...

GodOfNeoN
  • 45
  • 7

1 Answers1

4

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.

sevavietl
  • 3,762
  • 1
  • 14
  • 21
  • Thank you for your reply. I have now renamed the file to DB.php, but it still does not work. Do you have any other suggestion? Should I place the whole `"App\\Model\\"`: in my composer.json? – GodOfNeoN Mar 31 '18 at 15:41
  • Indeed, I still have the same error. `Class 'App\Model\DB' not found` – GodOfNeoN Mar 31 '18 at 15:43
  • `$ ls src/app/Model` --> `Alert.php CalenderActivity.php Category.php DB.php Leader.php Parsedown.php Registration.php User.php` – GodOfNeoN Mar 31 '18 at 15:44
  • @GodOfNeoN, have you called `composer dump-autoload` command to regenerate the autoload files? – sevavietl Mar 31 '18 at 15:45
  • I did perform that command yes. The result is - just like above in the question - that I have this in the generated file (`vendor/composer/autoload_psr4.php`): ``'App\\' => array($baseDir . '/src/app'),`` – GodOfNeoN Mar 31 '18 at 15:47
  • I should be punching myself... You did indeed solve my problem. I tested something out with another namespace name, and forgot to change that – GodOfNeoN Mar 31 '18 at 15:50
  • @GodOfNeoN, glad to help – sevavietl Mar 31 '18 at 15:51