2013-11-21 45 views
0

我剛剛將我的第一個composer.json包添加到了我創建的API。創建我的第一個composer.json文件

https://github.com/elvanto/api-php

這是我的文件的副本

{ 
    "name": "elvanto/api-php", 
    "type": "library", 
    "description": "API PHP Library for elvanto church management software.", 
    "keywords": ["elvanto", "api"], 
    "homepage": "https://github.com/elvanto/api-php/", 
    "license": "MIT", 
    "authors": [ 
     { 
      "name": "Ben Sinclair", 
      "homepage": "http://elvanto.com", 
      "role": "Developer" 
     } 
    ], 
    "require": { 
     "php": ">=5.3.0" 
    }, 
    "autoload": { 
     "classmap": [ 
      "." 
     ] 
    } 
} 

當我試圖用Composer來安裝包我

終端
-$ composer install 
Loading composer repositories with package information 
Installing dependencies (including require-dev) 
Nothing to install or update 
Generating autoload files 

在我的供應商/文件夾中得到這個,它加載自動加載文件,但不包括我的班級文件:

autoload.php 
composer/autoload_classmap.php 
composer/autoload_namespaces.php 
composer/autoload_real.php 
composer/ClassLoader.php 

我做錯了什麼?我試圖改變composer.json文件到以下(這是我見過的其他包做),但我得到了一個完全不同的錯誤:

"autoload": { 
    "classmap": [ 
     "elvanto_API.php" 
    ] 
} 

錯誤是:

[RuntimeException] 
Could not scan for classes inside "elvanto_API.php" which does not appear to be a file nor a folder 

我想我'm close我只需要在這一點上的一個小方向:)

回答

1

你第一次就明白了。 classmap部分只是可以找到想要自動加載的類的目錄列表。您無需在此部分中指定單個類名稱。

如果你看看你產生供應商/作曲/ autoload_classmap.php文件,你會看到它撿了起來:

$ cat vendor/composer/autoload_classmap.php 
<?php 

// autoload_classmap.php @generated by Composer 

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

return array(
    'elvanto_API' => $baseDir . '/elvanto_API.php', 
); 

現在你可以使用這個類在自動裝載風格:

<?php 

require_once 'vendor/autoload.php'; 

$test = new elvanto_API();