dream_life5200 2016-08-29 15:18
浏览 106

Composer和Linux生产服务器 - 自动加载不起作用

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.


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! :)

  • 写回答

2条回答 默认 最新

  • duandeng1824 2016-08-29 18:05

    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": [

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了