I have created a package to separate out business logic into easier to distribute modules. The composer file looks like this:

{
    "name": "aggiq/johnny-cash",
    "description": "A collection of controllers, models, migrations, and tests for a phonebanking backend.",
    "license": "MIT",
    "authors": [ ... ],
    "require": {
        "illuminate/database": ">=5.5"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4"
    },
    "autoload": {
        "psr-4": {
            "Johnny\\Phonebanking\\": "src/"
        }
    }
}

And our source files are indeed in src/:

src/Controllers/PhonebankController.php src/Models/Phonebank.php ...

I saved and pushed this to our gitlab repo, and then included it as a dependency in a test project:

{
    ...,
    "repositories": [{
        "type": "package",
        "package": {
            "name": "aggiq/johnny-cash",
            "version": "0.1",
            "type": "package",
            "source": {
                "url": "gitlab url",
                "type": "git",
                "reference": "dev"
            }
        }
    }],
    "require": {
        "aggiq/johnny-cash": "*",
    },
    ...
}

And when I do composer update, it successfully grabs the project and downloads it into the vendor folder:

vendor/aggiq/johnny-cash/Controllers/PhonebankController.php
...

However, when I look in the test project's autoload_psr4.php, it's not there. Is there a step I missed?

Edit: updates the directories to have capital letters to match the namespaces, and here is the generated PSR4 php file:

<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
);

You have registered autoloading at your package's composer.json correctly:

"autoload": {
   "psr-4": {
       "Johnny\\Phonebanking\\": "src/"
   }
}

This means, any class in Johnny\Phonebanking namespace will be in src directory. E.g.:

  • Johnny\Phonebanking\SomeClass => src/SomeClass.php
  • Johnny\Phonebanking\SomeNamespace\AnotherClass => src/SomeNamespace\AnotherClass.php

As you can see, it has to respect CapitalLetters.


Saying that, you should correct first letters of your directories, from:

src/controllers/PhonebankController.php
src/models/Phonebank.php

to

src/Controllers/PhonebankController.php
src/Models/Phonebank.php
  • I have renamed the directories to src/Controllers, src/Models, etc as you suggested, and reimported the project into my base project. I can confirm the directories in the vendor/ folder. However running composer update and composer dump-autoload do not add the classes to the psr4 autoload file. Sorry! – whiterook6 Oct 24 '17 at 21:15
  • Only prefixes and their locations shoudl be in autoload_psr4.php, not specific files. If "Johnny\\Phonebanking\\" is missing there, add the content of the file to your question please. – Tomáš Votruba Oct 24 '17 at 21:56
  • edited to include the PSR4 file's contents – whiterook6 Oct 25 '17 at 17:38
  • Could you temporarily allow access to the package? I want to test it. Also, is your problem really in empty autoload_psr4.php or that classes are not autoloaded? Have you tried them in your code? I ask because it might not be related, as composer has many (optimized) ways to load content. – Tomáš Votruba Oct 25 '17 at 21:00
up vote 0 down vote accepted

I have solved it. We needed to do two things.

  1. The repository type specified in the parent pacakge should be vcs not package since we are loading from a git server:

    "repositories": [{ "type": "vcs", "url": "git@xxx.git" }]

  2. The package type in the child package should be library:

    "type": "library"

Once those two changes were made, composer update installed not only the child package but also its dependencies, proving it is being recognized by composer.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.