沃梦达 / 编程问答 / php问题 / 正文

作曲家Linux 生产服务器 - 自动加载不起作用

Composer amp; Linux production server - autoload not working(作曲家Linux 生产服务器 - 自动加载不起作用)

本文介绍了作曲家Linux 生产服务器 - 自动加载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试搜索这个问题并看到了几个答案,但没有运气......

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

我已经用 Slim Framework v3 安装了 composer.

I have composer installed with Slim Framework v3.

我在 composer.json 文件中使用 PSR-4 自动加载我的文件,如下所示:

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

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

这是我的文件夹结构:

我使用 Apache 2.4 在 localhost Mac OS X El-Capitan 上运行它,一切都像魔术一样工作.但是当我将它上传到我的生产 Linux 服务器(也使用 Apache 2.4)时,自动加载似乎非常混乱,我收到了如下错误:

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:

警告:包含(/home/friendsapp/public_html/vendor/composer/../../app/Middleware/AuthMiddleware.php):无法打开流:/home/friendsapp/public_html 中没有这样的文件或目录/vendor/composer/ClassLoader.php 在第 412 行

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

警告:include():无法打开/home/friendsapp/public_html/vendor/composer/../../app/Middleware/AuthMiddleware.php"以包含(include_path='.:/usr/lib/php:/usr/local/lib/php') 在/home/friendsapp/public_html/vendor/composer/ClassLoader.php 第 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

致命错误:在第 5 行的/home/friendsapp/public_html/public/index.php 中找不到类 'AppMiddlewareAuthMiddleware'

Fatal error: Class 'AppMiddlewareAuthMiddleware' 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 AppMiddleware;

use PsrHttpMessageServerRequestInterface as Request;
use PsrHttpMessageResponseInterface as Response;

use AppMiddlewareMiddleware;

use AppShareErrorCode;
use AppModelsResultMessage;

use AppMappersAccessTokenMapper;

class AuthMiddleware extends Middleware {

任何帮助将不胜感激!:)

Any help would be most appreciated! :)

推荐答案

查看错误中的路径/app/Middleware/AuthMiddleware.php

看来这个问题是由 App\ 在您的生产环境中指向 /app 的命名空间冲突引起的,而不是您的 PSR-4 声明指向/App.

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.

为避免冲突并映射指定目录的所有命名空间,您可以在 composer 中使用自动加载 classmap 或配置 optimize-autoloader(可选)选项.json 用于定义指定目录中所有文件和对象的物理路径,以供作曲家加载.此外,使用 PSR-4 声明,将尝试从 App 命名空间路径声明中加载类映射路径中未找到的任何文件.例如,当使用 exclude-from-classmap 选项时.

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/",
    ],
}

在您的 composer.json 中进行更改后,请务必在您的开发环境中运行 php composer.phar update --lock.

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

然后将composer.lockcomposer.json文件上传到你的生产环境后,运行php composer.phar install --no-dev -ophp composer.phar dump-autoload --no-dev -o 来自生产环境.

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.

-o 选项将强制 optimize-autoloader 类映射运行,--no-dev 将阻止开发包(require-dev) 被安装.建议在生产环境中使用 optimize-autoloader.

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.

作为一般做法,每当您将开发更改部署到生产环境时,您都需要运行 php composer.phar install --no-dev -o 请参阅 使用 Composer 的开发/生产切换时如何正确部署?.这样,使用 php composer.phar update 从您的开发环境应用的更改将正确安装在您的生产环境中.

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.

这篇关于作曲家Linux 生产服务器 - 自动加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:作曲家Linux 生产服务器 - 自动加载不起作用