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

为什么我的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

图片转代码服务由CSDN问答提供 功能建议

我一直在测试我的包到达Packagist的一个简单的类。 每当我在一个不同的项目中需要它时,就说无法找到这个类。 我的composer.json autoload 块有问题吗?

这是我的项目仓库文件结构 \ n

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

这是我的 composer.json

  {
“name”:“prodikl / simple-view”,
“description”:“简单到 使用,基本的View对象。“,
”require“:{
”php“:”&gt; = 5.3.0“
},
”autoload“:{
”psr-4“:{”  prodikl“:”src /“} 
} 
} 
   
 
 

最后,在我的 View.php 中:

 &lt;?php 
 
namespace prodikl; 
 
class查看
 {
 ... 
} 
   
 \  n 

但每当我要求它进入一个项目并且需要“vendor / autoload.php”并使用时使用prodikl \ View; 它一直说不找到< / p>

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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/"}
    
    打赏 评论

相关推荐 更多相似问题