2

I have already tried searching for this question and seen a couple of answers, but no luck...

I have composer installed with Slim Framework v3.

I am using autoload for my files using PSR-4 in the composer.json file like this:

"autoload": {
 "psr-4": {
   "App\\": "App"
 }
}

And this is my folder structure:

enter image description here

I am running it on a localhost Mac OS X El-Capitan using Apache 2.4 and everything works like magic. But when I upload it to my Production Linux server (also with Apache 2.4), the autoload seems to be extremely confused and I am getting errors like these:

Warning: include(/home/friendsapp/public_html/vendor/composer/../../app/Middleware/AuthMiddleware.php): failed to open stream: No such file or directory in /home/friendsapp/public_html/vendor/composer/ClassLoader.php on line 412

Warning: include(): Failed opening '/home/friendsapp/public_html/vendor/composer/../../app/Middleware/AuthMiddleware.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/friendsapp/public_html/vendor/composer/ClassLoader.php on line 412

Fatal error: Class 'App\Middleware\AuthMiddleware' not found in /home/friendsapp/public_html/public/index.php on line 5

I have namespaced my classes exactly according to my folder structure.

<?php

namespace App\Middleware;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

use \App\Middleware\Middleware;

use \App\Share\ErrorCode;
use \App\Models\ResultMessage;

use \App\Mappers\AccessTokenMapper;

class AuthMiddleware extends Middleware {

Any help would be most appreciated! :)

Erez Hod
  • 1,813
  • 2
  • 23
  • 47

2 Answers2

12

Looking at the path in the errors /app/Middleware/AuthMiddleware.php

It appears the issue is caused by a namespace conflict of App\\ being pointed to /app in your production environment as opposed to your PSR-4 declaration pointing to /App.

To avoid conflicts and map all of the namespaces of a specified directory you can use the autoload classmap or config optimize-autoloader (optional) options in composer.json in order to define the physical path of all the files and objects in the specified directories for composer to load. Additionally with the PSR-4 declaration, any files not found in the classmap paths will be attempted to be loaded from the App namespace path declaration(s). For example when using the exclude-from-classmap option.

"config": {
    "optimize-autoloader": true
},
"autoload": {
    "psr-4": {
        "App\\": "App/"
    },
    "classmap": [
        "App/",
    ],
}

After making the change in your composer.json, be sure to run php composer.phar update --lock in your development environment.

Then after uploading the composer.lock and composer.json files to your production environment, run php composer.phar install --no-dev -o or php composer.phar dump-autoload --no-dev -o from the production environment.

The -o option will force the optimize-autoloader classmapping to run and --no-dev will prevent the development packages (require-dev) from being installed. Using optimize-autoloader is recommended for production environments.


As a general practice, anytime you deploy your development changes to your production environment you need to run php composer.phar install --no-dev -o See How to deploy correctly when using Composer's develop / production switch?. This way the changes applied from your development environment using php composer.phar update are installed in your production environment correctly.

Community
  • 1
  • 1
Will B.
  • 17,883
  • 4
  • 67
  • 69
  • 1
    That is actually.. a great answer, solved my problem! I logged in via SSH and ran "php composer.phar dump-autoload -o" and everything seems to work fine. Also thanks for the general practices, I will definitely adopt them. – Erez Hod Aug 29 '16 at 20:05
  • Keep in mind composer.phar will automatically run in `--dev` mode, so unless you need the `require-dev` dependencies in your production environment *(such as phpunit, phpspec, behat, and codesniffer, etc)*, you would benefit from adding the `--no-dev` switch to avoid installing those dependencies (and otherwise possibly open yourself to potential vulnerabilities with development libraries). – Will B. Aug 29 '16 at 20:42
  • Thank you very much – Sahadev Aug 11 '17 at 12:43
  • --no-dev -o saved me! – Haroldo May 29 '18 at 09:16
  • I add `classmap` to composer.json and run `dump-autoload -o` and it just worked. – Nixon Kosgei Jan 25 '20 at 08:46
1

For my production server the following worked:

composer install --no-dev -o

then restart php

on serverpilot:

rm -rf vendor/*
composer5.6-sp install --no-dev -o
sudo service php5.6-fpm-sp restart
Haroldo
  • 36,607
  • 46
  • 127
  • 169
  • Just composer command works for me. But I have to run this command every time after creating a new class on a production server, not needed for a local server. – Kabir Hossain Oct 22 '19 at 08:26