dongmin3754 2017-04-16 17:40
浏览 312
已采纳

为什么我的Composer包不能自动加载?

I've been testing getting my packages up to Packagist with a simple class I made. Whenever I require it in a different project, it says the class cannot be found. Is something wrong with my composer.json autoload block?

Here's my project repo file structure:

- src
    - prodikl
        - View.php
- .gitignore
- composer.json

And here's my composer.json:

{
  "name":"prodikl/simple-view",
  "description":"A simple to use, basic View object.",
  "require" : {
    "php" : ">=5.3.0"
  },
  "autoload": {
    "psr-4": {"prodikl": "src/"}
  }
}

And finally, in my View.php:

<?php

namespace prodikl;

class View
{
    ...
}

But whenever I require it into a project and do require "vendor/autoload.php" and use use prodikl\View; it it keeps saying not found

  • 写回答

1条回答 默认 最新

  • dorv56831 2017-04-16 17:46
    关注

    You just need to point your autoloader down one more directory:

      "autoload": {
        "psr-4": {"prodikl": "src/prodikl/"}
      }
    

    This means "classes that belong to the \prodikl namespace can be found in the src/prodikl/ directory."

    You might need trailing backslashes on the namespace name too, not sure how picky Composer is about it:

    "psr-4": {"prodikl\\": "src/prodikl/"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 求日版华为b610s-77a 官方公版固件,有偿
    • ¥15 关于#java#的问题,请各位专家解答!(相关搜索:java程序)
    • ¥15 连接所有楼的最短路径长度
    • ¥15 linux tsi721的驱动编译后 insmod 提示 报错
    • ¥20 multisim测数据
    • ¥15 求无向连通网的所有不同构的最小生成树
    • ¥15 模拟器的framebuffer问题
    • ¥15 opencv检测轮廓问题
    • ¥15 单点式登录SSO怎么爬虫获取动态SSO_AUTH_ACCESS_Token
    • ¥30 哈夫曼编码译码器打印树形项目