作曲家Linux生产服务器-自动加载不起作用 [英] Composer & Linux production server - autoload not working

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

问题描述

我已经尝试搜索此问题,并看到了一些答案,但是没有运气...

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

我在Slim Framework v3中安装了作曲家.

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在本地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:

警告:include(/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//home/friendsapp/public_html/vendor/composer/ClassLoader.php在412行上的php:/usr/local/lib/php')

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中找不到类"App \ Middleware \ AuthMiddleware"

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

推荐答案

查看错误/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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆