简体   繁体   English

Composer没有加载我自己的psr-0文件

[英]Composer is not loading my own psr-0 files

I can't seem to get composer to work with my own classes/files using the psr-0 autoload mechanism. 我似乎无法使用psr-0自动加载机制让作曲家使用我自己的类/文件。 Can anyone please shed some light as to why the below isn't working? 任何人都可以说明为什么以下不起作用?

I'm getting the following output in my error log: 我在错误日志中收到以下输出:

PHP Fatal error: Class 'TestdirTest1' not found in /home/webroot/bitlama/index.php on line 5 PHP致命错误:第5行/home/webroot/bitlama/index.php中找不到类'TestdirTest1'

It does work If I uncomment the explicit require statement (index.php:2). 它确实有效如果我取消注释显式require语句(index.php:2)。

And if anybody is wondering - yes I have run composer install in the form of: 'php ../composer.phar install'. 如果有人想知道 - 是的,我已经以'php ../composer.phar install'的形式运行composer install。

This is my directory structure: 这是我的目录结构:

├── composer.json
├── index.php
├── testspacedir
│   └── Testdir
│       └── test1.php
└── vendor
    ├── autoload.php
    └── composer
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_real.php
        └── ClassLoader.php

composer.json: composer.json:

{
    "autoload": {
        "psr-0": { "Testdir\\": "testspacedir/"}
    }
}

test1.php: test1.php:

<?php

namespace Testdir;

class Test1 {

    public function __construct()
    {
        echo "Woohoo Test1";
    }

}

index.php: index.php文件:

<?php
require 'vendor/autoload.php';
//require 'testspacedir/Testdir/test1.php';

$test1 = new Testdir\Test1();

vendor/autoload.php: 供应商/ autoload.php:

<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit7b4760e0f7ca9d6454dd2f098c374ba4::getLoader();

我的类文件被命名为test1.php ,而不是所需要的PSR-0命名约定Test1.php

You say it works because you removed require 'testspacedir/Testdir/test1.php'; 你说它有效,因为你删除了require 'testspacedir/Testdir/test1.php'; and that is correct. 这是正确的。

Since you defined the namespace -> folder structure in the autoload in composer.json , the vendor/autoload.php handles the loading of these folder(s) for you. 由于您在composer.json中的autoload中定义了命名空间 - >文件夹结构,因此vendor/autoload.php为您处理这些文件夹的加载。

Take a look inside that vendor/autoload.php file and you will see for yourself. 看看那个vendor/autoload.php文件,你会亲眼看看。

To sum it up, composer handles the autoloading of the files for you so you don't have to perform these includes. 总结一下,composer会为您处理文件的自动加载,因此您不必执行这些包含。 Here is a snippet from http://getcomposer.org/doc/01-basic-usage.md#autoloading 以下是http://getcomposer.org/doc/01-basic-usage.md#autoloading的片段

Note: Composer provides its own autoloader. 注意:Composer提供自己的自动加载器。 If you don't want to use that one, you can just include vendor/composer/autoload_namespaces.php, which returns an associative array mapping namespaces to directories. 如果您不想使用那个,可以只包含vendor / composer / autoload_namespaces.php,它返回一个关联数组映射名称空间到目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM