简体   繁体   English

作曲家和PSR-0

[英]Composer and PSR-0

I have composer.json structure like this: 我有这样的composer.json结构:

"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: 我在app / database / seeds中有文件(此处的文件可以动态创建,因此使用“ classmap”解决方案:[“ app / database / seeds”]在这里不起作用bcs我必须始终在播种前自动转储自动加载:

  • UserTableSeeder.php UserTableSeeder.php
  • DatabaseSeeder.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 安装php composer.phar和php composer.phar dump-autoload之后,我无法在应用程序中使用,例如在index.php中

 $object = new \DatabaseSeeder\DatabaseSeeder();

because I recieve error: Class DatabaseSeeder\\DatabaseSeeder does not exist WHY ?? 因为我收到错误:类DatabaseSeeder \\ DatabaseSeeder不存在,为什么? It should autoload class while initiating object. 它应该在启动对象时自动加载类。

First of all, you should make sure your index.php properly includes composer autoload. 首先,您应确保index.php正确包含composer自动加载。

<?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配置是

"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. 有关composer PSR-0自动加载的更多信息,您可以阅读composer 模式文档。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM