2013-12-07 33 views
6

我似乎無法讓作曲家使用psr-0自動載入機制來處理我自己的類/文件。任何人都可以請闡明爲什麼下面的不工作?作曲家不加載我自己的PSR-0文件

我得到以下輸出在我的錯誤日誌:

PHP Fatal error: Class 'TestdirTest1' not found in /home/webroot/bitlama/index.php on line 5

它的工作。如果我取消了明確的要求聲明(的index.php:2)。

如果有人想知道 - 是的,我已經運行作曲者安裝形式:'php ../composer.phar安裝'。

這是我的目錄結構:

├── 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:

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

test1.php:

<?php 

namespace Testdir; 

class Test1 { 

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

} 

的index.php:

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

$test1 = new Testdir\Test1(); 

供應商/ autoload.php:

<?php 

// autoload.php @generated by Composer 

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

return ComposerAutoloaderInit7b4760e0f7ca9d6454dd2f098c374ba4::getLoader(); 
+0

查看'vendor/composer/autoloader_namespaces.php'。這應該包含一行與您的名稱空間作爲數組鍵和正確的文件夾。 – Sven

回答

3

片段我的類文件被命名爲test1.php,而不是Test1.php所需的PSR-0命名約定。

+0

馬克這一個作爲你的答案,如果它是。 – Richard

0

你說這樣的作品,因爲你刪除require 'testspacedir/Testdir/test1.php';,並且是正確的。

由於您在autoloadcomposer.json中定義了名稱空間 - >文件夾結構,因此vendor/autoload.php會爲您處理這些文件夾的加載。

看看那vendor/autoload.php文件裏面,你會看到自己。總結一下,作曲家爲你處理文件的自動加載,所以你不必執行這些包含。下面是從http://getcomposer.org/doc/01-basic-usage.md#autoloading

Note: Composer provides its own autoloader. 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.

+0

我的意思是說,只有在我明確要求聲明的情況下才有效。否則不起作用 - 我得到一個致命的錯誤。 –

+0

我很困惑的話,你手動通過'autoload.php'不見了,看看是否一切正常,所有的路徑設置是否正確? – SamV

+0

不肯定是很誠實的。我已經編輯與autoload.php –