且构网

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

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

更新时间:2023-11-17 11:09:04

查看错误/app/Middleware/AuthMiddleware.php

问题似乎是由于生产环境中指向/app的名称空间冲突App\\而不是指向/App的PSR-4声明引起的.

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.json中使用autoload classmap或config optimize-autoloader(可选)选项,以定义所有文件和对象的物理路径.在指定目录***作曲家加载.此外,对于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.