Composer autoload_namespaces数组为空,但它正在下载我的包

时间:2017-02-11 22:04:19

标签: php composer-php

我在Windows上使用composer,当我执行composer installcomposer update时,它正在成功加载我在composer.json文件中使用的2个包。

{
    "require": {
        "bcosca/fatfree": "dev-master",
        "respect/validation": "^1.1"
    }
}

我的index.php中的第一行是

require_once __DIR__ . '\vendor\autoload.php';

这似乎没有加载任何东西,例如,与这两个包中未定义的类相关的许多错误。我进一步调查了一下,发现我的autoload_namespaces.php如下:

<?php

// autoload_namespaces.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
);

数组完全是空的,这是怎么回事?我希望能看到这2个套餐&#39;这里的要求......

感谢您的帮助!

从包本身添加composer.json: F3:

{
    "name": "bcosca/fatfree",
    "description": "A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!",
    "homepage": "http://fatfreeframework.com/",
    "license": "GPL-3.0",
    "require": {
        "php": ">=5.3.6"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/bcosca/fatfree"
        }
    ],
    "autoload": {
        "files": ["lib/base.php"]
    }
}

验证

{
    "name": "respect/validation",
    "description": "The most awesome validation engine ever created for PHP",
    "keywords": ["respect", "validation", "validator"],
    "type": "library",
    "homepage": "http://respect.github.io/Validation/",
    "license": "BSD Style",
    "authors": [
        {
            "name": "Respect/Validation Contributors",
            "homepage": "https://github.com/Respect/Validation/graphs/contributors"
        }
    ],
    "require": {
        "php": ">=5.4",
        "symfony/polyfill-mbstring": "^1.2"
    },
    "require-dev": {
        "egulias/email-validator": "~1.2",
        "malkusch/bav": "~1.0",
        "mikey179/vfsStream": "^1.5",
        "phpunit/phpunit": "~4.0",
        "symfony/validator": "~2.6.9",
        "zendframework/zend-validator": "~2.3"
    },
    "suggest": {
        "ext-bcmath": "Arbitrary Precision Mathematics",
        "ext-mbstring": "Multibyte String Functions",
        "egulias/email-validator": "Strict (RFC compliant) email validation",
        "malkusch/bav": "German bank account validation",
        "symfony/validator": "Use Symfony validator through Respect\\Validation",
        "zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation",
        "fabpot/php-cs-fixer": "Fix PSR2 and other coding style issues"
    },
    "autoload": {
        "psr-4": {
            "Respect\\Validation\\": "library/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Respect\\Validation\\": "tests/library/"
        }
    },
    "extra": {
        "branch-alias": {
            "dev-master": "1.1-dev"
        }
    },
    "scripts": {
        "test": "./vendor/bin/phpunit"
    }
}

在加载Validator之后,这个Symfony组件附带了它:

{
    "name": "symfony/polyfill-mbstring",
    "type": "library",
    "description": "Symfony polyfill for the Mbstring extension",
    "keywords": ["polyfill", "shim", "compatibility", "portable", "mbstring"],
    "homepage": "https://symfony.com",
    "license": "MIT",
    "authors": [
        {
            "name": "Nicolas Grekas",
            "email": "p@tchwork.com"
        },
        {
            "name": "Symfony Community",
            "homepage": "https://symfony.com/contributors"
        }
    ],
    "require": {
        "php": ">=5.3.3"
    },
    "autoload": {
        "psr-4": { "Symfony\\Polyfill\\Mbstring\\": "" },
        "files": [ "bootstrap.php" ]
    },
    "suggest": {
        "ext-mbstring": "For best performance"
    },
    "minimum-stability": "dev",
    "extra": {
        "branch-alias": {
            "dev-master": "1.3-dev"
        }
    }
}

2 个答案:

答案 0 :(得分:4)

  

数组完全是空的,这是怎么回事?我希望能看到这2个套餐&#39;这里的要求......

  • Composer支持多种类加载方法。一个重要的实现PSR4规范。 SymfonyRespect正在使用PSR4。您可以查看他们的composer.json文件或生成的vendor/composer/autoload_psr4.php文件来验证它。

  • Fat-Free Framework允许Composer 自动加载lib/base.php文件。这一点很重要,因为包括base.php两次会抛出PHP Fatal error: Cannot declare class Cache, because the name is already in use。解决方案是让Composer完成工作并取而代之的是$f3 = Base::instance()

<强>建议

  • 删除vendor文件夹并使用Composer进行全新安装

  • 验证代码中是否缺少(或导入)类名称空间,例如\Respect\Validation\Validator代替Validator

答案 1 :(得分:0)

问题在于您的require_once()行。它没有给autoload.php提供正确的文件路径。如果您的'vendor'目录与index.php处于同一级别,那么您只需要自动加载器,如下所示:

enter image description here

<?php
require_once 'vendor/autoload.php';

echo 'Loaded...';

请注意,autoload_namespaces.php完全没有问题。我也和你的一样。