duandong1869
duandong1869
2015-05-04 21:49

使用composer加载git repo - 自动加载问题

已采纳

I have a github repository https://github.com/KoulSlou/UPS and I would like to add it to my project.

In project root I created composer.json file and defined the following autoloading properties:

{
    "autoload": {
        "files": [
            "libraries/Ups/Ups.php",
            "libraries/Ups/Ups_Base.php",
            "libraries/Ups/Ups_Base_Response.php",
            "libraries/Ups/Ups_Live_Rates.php"
        ]
    }
}

When I run

php composer.phar install 

repository is being downloaded, but it looks like autoloader is not working. When I try to initialize one of the classes

$test = new Ups()

I got the following error:

Fatal error: Class 'Ups' not found in application/....

Did I define "autoload" property incorrectly?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongmale0656 dongmale0656 6年前

    Finaly, I have found out what was the problem. composer.json file in the project I was trying to load - UPS library -was invalid. I was able to download files when I ran:

    composer.phar install
    

    but it looks like composer.json file was ignored. I found it out when I ran

    composer.phar update
    

    and got

    No valid composer.json was found
    

    With option -v I got error that "name" is undefined index. So, I simply added "name" field to the composer.json. Final version is:

    {
        "name":"KoulSlou/UPS",
        "autoload": {
            "files": [
                "libraries/Ups/Ups.php",
                "libraries/Ups/Ups_Base.php",
                "libraries/Ups/Ups_Base_Response.php",
                "libraries/Ups/Ups_Live_Rates.php"
             ]
        }
    }
    
    点赞 评论 复制链接分享
  • douhe6181 douhe6181 6年前

    I'd suggest not using the "files" autoloader, because that isn't very automatic - the files mentioned here are ALWAYS included. Replacing it with "classmap" would be better. And then you'd not be required to mention ALL files, but you can simply state the directory you want to have scanned for classes.

    Now what I don't see anywhere: Did you initialize Composer's autoloader anywhere? This usually is something like

    require "vendor/autoload.php";
    
    点赞 评论 复制链接分享

为你推荐