部署后,Composer autoload无法正常工作

时间:2015-11-10 20:19:22

标签: php composer-php autoload

使用composer和autoload组织我的代码相对较新。我有一个git存储库,在我的本地机器上,我在我的项目的根目录中设置了composer。我指定了要运行的composer.json中的所有内容。使用" composer install",所有库都会自动安装。

{
    "name": "my/repo",
    "description": "bla",
    "version": "1.2.3",
    "require":
    {
        "php": "5.6.*",
        "geraintluff/jsv4": "1.*",
        "lcobucci/jwt": "^3.0"
    },
    "autoload":
    {
        "psr-4":
        {
            "MyNamespace\\": "src/"
        }
    }
}

所以 - 一旦我跑了" composer install"在我的本地机器上,一切都在我的代码中自动加载。细

但是现在我需要在另一个Linux系统上部署整个东西。所以我从git中拉出并运行composer install。获取所有库,并在vendor/

中显示自动加载文件

然而,我不能使用自动加载(是的,我做require_once(__DIR__ . '/../vendor/autoload.php');)。每当我尝试实例化一个类时,我都会得到一个

PHP Fatal error:  Class 'X' not found in /var/www/bla/x.class.php on line 123

使用use X;无法解决问题,也没有尝试使用其完整的命名空间名称来实例化该类(例如$x = new \A\B\X();

这是文件夹结构(如果这很重要):

+ src/
| + X.class.php // namespace here is "MyNamespace"
| + Y.class.php // same namespace
+ test/
  + run.php // namespace is "Test"

以下是此代码的一个片段(run.php):

<?php namespace Test; // different namespace than the rest of the code
// making the namespace also "\MyNamespace" wouldnt work either

require_once(__DIR__ . '/../vendor/autoload.php');

use \MyNamespace\Y; // whether this line is here or not does not change the error

session_start();

// same error as with "just" implements Y {}
class SomeClass implements \MyNamespace\Y {
    // ...
}

?>

所以在这里,Y延长的行会抛出致命错误。无论我是否使用完整命名空间。唯一有帮助的是require_once()......

所以,这迫使我回到执行所有要求/包括我自己的繁琐方式!?有什么方法可以解决这个问题吗?

PS:composer dumpautoload不会帮助

PPS:composer validate显示没有错误

1 个答案:

答案 0 :(得分:1)

对于PSR-4合规性,您的文件结构应为:

+ src/
| + X.php
| + Y.php

请注意删除.class.php后缀。 http://www.php-fig.org/psr/psr-4/

相关问题