且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-17 10:15:46

查看错误中的路径/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.