Composer autoload psr 4 wildcard expansions for matching all modules?

I have this structure for my app,

  config/ (config and setup)
  module/ (tons of modules!)
     Article/ (one of the modules)
        source/ (<----- note that all php psr4 classes are stored in this folder)
     Book/ (one of the modules)
       ....(the structure is the same as Article's)
  public/  (web site doc root)
    ...(other packages)

And this is my composer.json,

    "require": {
        "slim/slim": "~2.0",
        "slim/views": "0.1.*"
    "autoload": {
        "psr-4": {
            "Foo\\": [
        "psr-0": {
            "": "library/"

As you can see that I have this "module/*/source/" in .json. Reason I do this is because I want to avoid writing every module out as it is going to be a long list. And it is not flexible too when I have new modules then I have to make changes to the .json.

But "module/*/source/" is not working. So can I do regex or something in this .json so that it is 'intelligent' enough to pick up all modules? Such as,


Is it possible?

Currently you cannot have wildcards in the directory path of an autoloading definition.

You can however adjust your project to allow having ANY class of a module being autoloaded by changing your directory structure. You have to get rid of all levels that are not mentioned in the fully qualified class name, i.e. no "source"

"Article/source/Article/Model/ArticleModel.php" hosting \Foo\Article\Model\ArticleModel would be required to live in "Article/Model/ArticleModel.php"

The always possible alternative is to mention all modules in the autoloading, or to split them into Composer packages on their own.

Remark regarding performance: Always try to use the longest prefix possible for autoloading. "psr-0": { "" : "library" } will force Composer to look into that library directory and search for a matching file FOR ALL CLASSES you load. Although Composer tries to optimize this by remembering failures, it still has an impact.

You should always add the prefixes that uniquely identify the location of a class, and you can add more than one prefix pointing to the same location for PSR-0. With PSR-4, the location differs by nature.

Think about writing a script that generates your autoloading definition.

Wildcard expansion in the autoload definition? � Issue #4383 , I have this structure for my app, myapp/ config/ (config and setup) module/ (tons of modules!) Article/ (one of the modules) source/ (<----- note that all php psr4� First of all if you are not familiar with PSR-4 but know about PSR-0 the main difference and benefit is that it allows for flatter directory structures in your git repositories. While you typically had src/Vendor/Lib/Class.php in libraries it is now possible to use a simpler src/Class.php while retaining the namespacing at the code level.

You can require this composer plugin. It allows the usage of wildcards:


Autoload files wildcarding � Issue #4742 � composer/composer � GitHub, I think Composer should have wildcard expansions in the autoload definition Article/ (one of the modules) source/ (<----- note that all php psr4� Learn how to install Composer on your machine. Understand the concept of Composer; Until now, you should have installed Composer and possibly created a composer.json file using composer init. If not, you can create one manually now in your project’s root. $ touch composer.json Setup PSR4 Autoloading

Here, should work


Note from your pattern "module/*/source/", the * here will also work well on bash shell, It's called globbing (pathname expansion).

Additional information

To done the path expansion in JSON you can use JSONPath which has an easy example here

XPath vs JSONPath

XPath: /store/*

JSONPath: $.store.*


PSR-4: Autoloader, This is a feature request for autoloading files by wildcard. The above example would recursively load all files under src matching functions.* (such as Related to #4383 which is the same request for psr-0/psr-4. Not really� This is a feature request for autoloading files by wildcard. Using fnmatch patterns, autoloading functions (and other non-class) definitions would be made considerably easier. "autoload": { "files": ["src/**/functions.*"]

Basic usage, Overview. This PSR describes a specification for autoloading classes from file paths. It is fully All class names MUST be referenced in a case-sensitive fashion. The subdirectory name MUST match the case of the sub-namespace names. Tenho lido, também, sobre PSR-0 vs PSR-4 e ainda estou um pouco em dúvidas de qual utilizar. Neste meu caso qual devo implementar, PSR-0 ou PSR-4 ? php composer dependências php-autoload

The composer.json Schema, To start using Composer in your project, all you need is a composer.json file. branches and tags) to attempt to find the best match for the version constraint you have Composer will register a PSR-4 autoloader for the Acme namespace. Call dump-autoload with -o / --optimize; What does it do?# Class map generation essentially converts PSR-4/PSR-0 rules into classmap rules. This makes everything quite a bit faster as for known classes the class map returns instantly the path, and Composer can guarantee the class is in there so there is no filesystem check needed.

[policy] Drupal and PSR-0/PSR-4 Class Loading, Under the psr-4 key you define a mapping from namespaces to paths, relative to the package root. When autoloading a class like Foo\\Bar\\Baz a namespace prefix For example Foo would match classes in the FooBar The PSR-4 references are all combined, during install/update, into a� 4. Classmap vs. PSR-0 vs. PSR-4. All of them are different ways to map the namespaces with their corresponding paths, once you choose one of them, you have to follow their rules and at the end

  • Thanks for the answer. I hope that Composer will have wildcard expansion in the autoload definition soon as it doesn't make sense while you can do that in "require", such as "slim/views": "0.1.*" but not in "autoload".
  • Create an issue on Github for it.
  • "Could not find a matching version of package teewurst/psr4-advanced-wildcard-composer-plugin." - would you mind to add to packagist?
  • Done -…
  • I think the answer maybe can not because json is a purely data object. Possible way maybe process it later using php.
  • Here tells you that JSON doesn't support regex data type which might imply that pathname expansion in JSON is impossible.
  • @teelou I found it you can use JsonPath which allow you to use path expansion.
  • Are you discussing the possibilities of a generic JSON being able to allow wildcard expansion? This doesn't matter at all, because the JSON is going to be used by Composer - and Composer does not offer the feature to have wildcard expansion in the autoload definition. If you want to have it, send a pull request - until then: It won't work.