Use Composer to autoload classes from outside the vendor

The larapen directory would be in your project root. Its on the same level as vendor directory is.

If you define:

"autoload": {
    "psr-4": {
        "Larapen\\MyLibraries\\": "larapen/MyLibraries/src/"

this will not load classes from /var/www/html/xxx/vendor/larapen/MyLibraries/src, like you stated.

Because your project structure is:

 +- /xxx (project)
     - composer.json
     +- /larapen
        +- MyLibraries
           +- src
     +- /vendor
        - autoload.php
        +- vendor-projectA
        +- vendor-projectB
        +- larapen

The \Larapen\MyLibraries namespace would map to classes inside

/var/www/html/xxx/larapen/MyLibraries/src and not inside


But how can I load classes from /var/www/html/xxx/?

Define the paths in the composer.json (inside /var/www/html/xxx/) of your project:

"autoload": {
    "psr-4": {
        "ProjectRoot\\" : "", 
        "NamspaceInSourceDir\\" : "larapen/"         

or load the composer autoloader in your index.php or during it’s bootstrap and add the paths manually:

$loader = require 'vendor/autoload.php';
$loader->add('Namespace\\Somewhere\\Else\\', __DIR__);
$loader->add('Namespace\\Somewhere\\Else2\\', '/var/www/html/xxx');