doulaopu2343 2013-12-21 10:25 采纳率: 100%
浏览 32
已采纳

作曲家和PSR-0

I have composer.json structure like this:

"psr-0": {
        "DatabaseSeeder\\": "app/database/seeds/"
    },

I have files in app/database/seeds (Files here can be created dynamically so solution with "classmap": ["app/database/seeds"] doesn't work here bcs I have to always dump-autoload before seeding:

  • UserTableSeeder.php
  • DatabaseSeeder.php

with structure like this:

# DatabaseSeeder.php

namespace DatabaseSeeder;

use Doctrine\ORM\EntityManager;

 class DatabaseSeeder implements Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(EntityManager $em)
    {
            $this->call('DatabaseSeeder\UserTableSeeder', $em);
    }

    private function call($class, $em)
    {
            $reflectionMethod = new \ReflectionMethod($class, 'run');
            $reflectionMethod->invoke(new $class, $em);
    }

After php composer.phar install and php composer.phar dump-autoload I cannot use in application for example in index.php

 $object = new \DatabaseSeeder\DatabaseSeeder();

because I recieve error: Class DatabaseSeeder\DatabaseSeeder does not exist WHY ?? It should autoload class while initiating object.

  • 写回答

1条回答 默认 最新

  • dtk31564 2013-12-21 11:16
    关注

    First of all, you should make sure your index.php properly includes composer autoload.

    <?php
    
    include __DIR__ . '/vendor/autoload.php';
    

    Then you need to validate your directory structure to be properly autoloaded.

    If your PSR-0 configuration is

    "psr-0": {
        "DatabaseSeeder\\": "app/database/seeds/"
    },
    

    then your directory structure should looks like:

    app/
       database/
           seeds/
               DatabaseSeeder/
                   DatabaseSeeder.php
    

    For more information about composer PSR-0 autoload, you can read composer schema documentation. Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计