dongye9991
2015-12-21 18:31
浏览 43
已采纳

为什么我必须在安装我的包后运行Composer转储?

I have created a simple package on packagist for learning (tommytest/tommytest). It installs fine, but I have to run "composer dump-autoload -o" immediately after it is installed. Before manually running the dump-autoload I get this:

Fatal error: Class 'mysqlgridspace\Page' not found in C:\xampp\htdocs\simple\index.php on line 5

After manually running dump-autoload it works fine. Isn't it supposed to handle the autoload set-up automatically when the package is installed?

Here's my composer.json (located in: simple/vendor/tommytest/tommytest/composer.json)

 {
  "name": "tommytest/tommytest",
  "type": "library",
  "description": "Framework agnostic data grid",
  "keywords": ["datagrid","data grid"],
  "homepage": "https://github.com/escalibore/tommytest",
  "license": "MIT",
  "authors": [
    {
      "name": "Tommy Bert",
      "email": "tom@tberthold.com",
      "homepage": "http://tberthold.com",
      "role": "Developer"
    }
  ],
  "require": {
    "php": ">=5.3.0"
  },
  "autoload": {
    "psr-4": {
      "mysqlgridspace\\":"src/"
    }
  }
}

And my class file (located in: simple/vendor/tommytest/tommytest/src/Mysqlgridmain.class.php)

<?php
namespace mysqlgridspace;

class Page {
    public function __construct()
    {
        echo "hello, i am a page.<br>";
    }
}

class Book {
    public function __construct()
    {
    echo "hello, i am a book.<br>";
    }
}

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

我在packagist上创建了一个用于学习的简单包(tommytest / tommytest)。 它安装得很好,但我必须在安装后立即运行“ composer dump-autoload -o ”。 在手动运行dump-autoload之前,我得到了这个:

致命错误:在C:\ xampp \ htdocs \ simple \ index.php中找不到类'mysqlgridspace \ Page' 第5行

手动运行dump-autoload后,它可以正常工作。 是不是应该在安装软件包时自动处理自动加载设置?

这是我的composer.json(位于: simple / vendor / tommytest / tommytest / composer.json

  {
“name”:“tommytest / tommytest”,
“type”:“library”,
“description”  :“框架无关数据网格”,
“关键字”:[“datagrid”,“数据网格”],
“主页”:“https://github.com/escalibore/tommytest",
”license“  :“麻省理工学院”,
“作者”:[
 {
“name”:“Tommy Bert”,
“email”:“tom@tberthold.com”,
“主页”:“http:/  /tberthold.com",n“角色”:“开发人员”
} 
 
,
“要求”:{
“php”:“&gt; = 5.3.0”
},
“  autoload“:{
”psr-4“:{
”mysqlgridspace \\“:”src /“
} 
} 
} 
   
 
 

我的班级文件(位于: simple / vendor / tommytest / tommytest / src / Mysqlgridmain.class.php

 &lt;?php  
namespace mysqlgridspace; 
 
class Page {
 public function __construct()
 {
  echo“你好,我是一个页面。&lt; br&gt;”; 
} 
} 
 
class Book {
 public function __construct()
 {
 echo“hello,我是一本书。&lt;  br&gt;“; 
} 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyixun0634 2015-12-25 15:07
    已采纳

    Those classes cannot be autoloaded with Composer's PSR-4 autoloader because it resolves the class name mysqlgridspace\Page to a file named Page.php, which should exist in src - it doesn't, though.

    First of all, there should only be one class declared in each file.

    You should have

    src/
        Book.php
        Page.php
    

    each declaring one class only.

    Then your PSR-4 autoloading should work.

    For reference, see http://www.php-fig.org/psr/psr-4/.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujiao1948 2015-12-21 20:30

    I found a way around the problem. I added "files": ["src/Mysqlgridmain.class.php"], to the autoload details in my composer.json

    So it went from:

    "autoload": {
       "psr-4": {
         "mysqlgridspace\\":"src/"
    }
    

    to:

    "autoload": {
      "files": ["src/Mysqlgridmain.class.php"],
      "psr-4": {
        "mysqlgridspace\\":"src/"
    } 
    

    I don't know why the psr-4 tag doesn't do it, but I think this is an acceptable work-around.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题