Search code examples
composer-phptypo3typo3-10.x

Autoloading of classes of a local TYPO3 extension


In my following composer.json I am requiring extensions, which are in the same Git repository as the whole project. So I add in the repositories section and later I do composer req vendor/site_package:@dev in order to require my local extension.

Now I realized, that some classes of the extension are not autoloaded.

Do I need to additional add the autoload part as shown below in the composer.json of the project?

{
    "name": "site-package",
    "description": "Base composer.json",
    "repositories": [
        {
            "type": "path",
            "url": "./packages/*"
        }
    ],
    "require": {
        "typo3/cms-backend": "^10.4",
        "typo3/cms-belog": "^10.4",
        "typo3/cms-beuser": "^10.4",
        "typo3/cms-core": "^10.4",
        ...
        "vendor/site_package": "@dev",
        "georgringer/news": "^8",
        ...
    },
    "autoload": {
        "classmap": [
            "public/typo3conf/ext/site_package/Classes"
        ],
        "psr-4": {
            "Vendor\\SitePackage\\": "public/typo3conf/ext/site_package/Classes"
        }
    },
    "extra": {
        "typo3/cms": {
            "root-dir": "public",
            "web-dir": "public"
        }
    },
    "config": {
        "vendor-dir": "vendor",
        "bin-dir": "bin"
    },
    "scripts": {
        "typo3-cms-scripts": [
            "typo3cms install:generatepackagestates",
            "typo3cms install:fixfolderstructure"
        ],
        "post-autoload-dump": [
            "@typo3-cms-scripts"
        ]
    }
}

In ext:site_package I have the following autoload section as well:

"autoload": {
    "psr-4": {
        "Vendor\\SitePackage\\": "Classes",
    }
},

Do I need both? Why?


Solution

  • You should never mix those 2 composer.json.

    As you already follow the approach of having a directory packages (which is a good thing) each extension inside there needs an own composer.json file which of course also needs a section

    "psr-4": {
       "Vendor\\MyExt\\": "Classes"
    }
    

    By requiring this package, this autoload information will be used.


    If you would still have the custom extension inside typo3conf/ext/my_ext, that composer.jsonfile would not be taken into account and you need something like

    "psr-4": {
       "Vendor\\MyExt\\": "typo3conf/ext/myext/Classes"
    }