5

I have my own little MVC framework and I use composer psr-4 autoloading.

On my own computer it works perfectly fine, but when I deployed it to my Ubuntu server it did not work anymore. (it doesn't find any classes anymore) I have tried a lot of things but it just won't work whatever I try...

What I have tried:

  • composer dump-autoload
  • composer update
  • removing everything and uploading again
  • searching on internet for a couple hours... :(

This is my composer.json:

{
  "autoload": {
    "psr-4": {
      "App\\": "app",
      "Core\\": "core",
      "Magister\\": "vendor/Magister"
    }
  },
  "require": {
    "philo/laravel-blade": "^3.1"
  }
}

I just don't get it why it's not working on my server.... I am using an other version of php on my server: 7.1, and I am using 5.6 on my computer, but this shouldn't make any difference right?

How do I fix this problem? I just don't get it why it happens.... :(

EDIT:

My code:

Index.php:

<?php

require "core/app.php";

$app = new \Core\App();

echo $app->start();

app.php:

<?php

namespace Core;

require "./vendor/autoload.php";

class App
{

    function start()
    {
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL ^ E_DEPRECATED);

        $MC = new Routing();
        // This is where it fails. Get the error: "class Core\Routing not found"

Routing.php:

<?php

namespace Core;

Use App\routes;

class Routing
{
    private $parameters = [];

    public function GetMC($Getroute){
    }

}

File structure on server:

enter image description here

I have excluded the vendor map from the tree

Luuk Wuijster
  • 6,678
  • 8
  • 30
  • 58
  • 1
    You haven't posted PHP code - did you `require_once` the `vendor/autload.php` correctly? Do you have error reporting enabled and turned all the way up? Set `error_reporting(E_ALL); ini_set('display_errors', 1)`; and see what php 7.1 says. There are differences but without seeing your code it is nearly impossible to say what could be going wrong. Maybe you have a dependency on an extension not present on the server. – Michael Berkowski Jan 15 '18 at 03:01
  • I don't think adding the php code will help. It works correctly on my computer. Of course I `require_once` the `vendor/autoloader.php` because if I didn't, how would it be working on my computer??? – Luuk Wuijster Jan 15 '18 at 03:29
  • 1
    I mean, if any paths are different on the server, the autoloader may not have been required. If you don't want to post code, that's fine - but it could help to spot issues that could fail in 7.1. At the very least, you must check PHP error messages. – Michael Berkowski Jan 15 '18 at 03:32
  • ....and better not only check error messages, but also post them – Nico Haase Jan 15 '18 at 08:20
  • Sorry that it took so long. But I have updated my question with more information. – Luuk Wuijster Jan 15 '18 at 14:06
  • You did execute composer install on the server, right? Did you check if the classes are maybe really missing? – Fels Jan 15 '18 at 14:07
  • Yes, I did. And all classes are present. – Luuk Wuijster Jan 15 '18 at 14:08
  • Help? Please??? – Luuk Wuijster Jan 15 '18 at 16:01

2 Answers2

9

okay... I have fixed it.

I have changed my composer.json to this:

{
  "autoload": {
    "psr-4": {
      "App\\": "app/",
      "Core\\": "core/",
      "Magister\\": "vendor/Magister/"
    },
    "classmap": [
      "app/",
      "core/",
      "vendor/Magister/"
    ]
  },
  "require": {
    "philo/laravel-blade": "^3.1"
  }
}
Luuk Wuijster
  • 6,678
  • 8
  • 30
  • 58
  • 1
    Do you happen to know why? – Sam Jun 12 '18 at 14:05
  • @Luuk Wuijster you are a lifesaver! I was stumped on this one for a long time with a deployment on AWS. It must be a PHP version thing, because we upgraded our server environment to v7 not too long ago. – ironic_ollins Jan 03 '19 at 20:04
  • 1
    @Luuk Wuijster Thanks a lot. @Sam this is because heroku deploys your code under `app` folder. So adding "app/" to classmap solved the issue – Amgad Dec 08 '21 at 11:56
  • I was so much worried after pushing to production and adding classmap solved the issue. Thank you. – Alaksandar Jesus Gene Sep 17 '22 at 02:24
5

If you want to use psr-4 you need to capitalize your directories to

app
- Modules
- Controllers
- Views
-- Layouts
...

Please refer to this post as to why your autoloading isn't working.

Sumner Erhard
  • 63
  • 1
  • 3
  • 2
    Thanks @sumner - this caught me out also. My framework was working fine on MacOS (which doesn't seem to care as much about case sensitivity in the file system), but your solution of capitalising directory names fixed it for me on Amazon Linux 2. – BSUK Nov 17 '21 at 20:15