Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Composer to autoload classes from outside the vendor?

I use psr-4 autoloader from composer:

"autoload": {
    "psr-4": {
        "DG\\Munchkin\\": "src/DG/Munch/"
    }
}

This loads classes from /var/www/html/xxx/vendor/yyy/src/DG/Munch

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

I wrote my own autoloader, but when I require vendor/autoload.php (composer autoload) and my autoloader, it won't work until I create instance of a class in my own autoloader.

like image 235
Tomasz Szymanek Avatar asked Jan 31 '15 15:01

Tomasz Szymanek


People also ask

How do you autoload with composer?

After you create the composer. json file in your project root with the above contents, you just need to run the composer dump-autoload command to create the necessary autoloader files. These will be created under the vendor directory. Finally, you need to include the require 'vendor/autoload.

How does PHP autoload work?

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.

What is composer dump-autoload?

composer dump-autoload. php artisan dump-autoload. It regenerates the list of all the classes that need to be included in the project (autoload_classmap. php). It will 'recompile' loads of files creating the huge bootstrap/compiled.php.

How do you load classes in PHP?

PHP can load class files automatically on demand (No explicit require statements are needed); The file name must match the case of the terminating class name (each class in a separate file); The directory name must match the case of the namespace names; __autoload() has been DEPRECATED as of PHP 7.2.


1 Answers

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

If you define

"autoload": {
    "psr-4": {
        "DG\\Munchkin\\": "src/DG/Munch/"
    }
}

this will not load classes from /var/www/html/xxx/vendor/yyy/src/DG/Munch, like you stated.

Because your project structure is:

/var/www/html/
 +- /xxx (project)
     - composer.json
     +- /src
        +- DG
           +- Munch
     +- /vendor
        - autoload.php
        +- vendor-projectA
        +- vendor-projectB
        +- yyy

The \DG\Munchkin namespace would map to classes inside

/var/www/html/xxx/src/DG/Munch and not inside

/var/www/html/xxx/vendor/yyy/src/DG/Munch.

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\\" : "src/"         
    }
 }

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');

Referencing: https://getcomposer.org/doc/04-schema.md#autoload

like image 54
Jens A. Koch Avatar answered Oct 21 '22 23:10

Jens A. Koch