Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer autoload - load class from parent directory

I'm currently working on a Laravel project that needs to access classes from its parent directory.

composer.json > PSR-4:

    "psr-4": {
        ...
        "ModuleA\\": "../ModuleA/baseObjects",
        "ModuleB\\": "../ModuleB/baseObjects"
    }

Example file structure:

/var/www
 +- /xxx (project)
     +- /ModuleA
        +- /baseObjects
            - configClass.inc
     +- /ModuleB
        +- /baseObjects
            - configClass.inc
     +- /laravel
        - composer.json

I run composer dump-autoload but the project still can't find ModuleA\configClass neither ModuleB\configClass.

Furthermore, inside my autoload_psr4.php, the above gets referenced as following:

'MobuleA\\' => array($baseDir . '/../MobuleA/baseObjects')
'MobuleB\\' => array($baseDir . '/../MobuleB/baseObjects')
like image 374
davidvnog Avatar asked Jul 07 '17 09:07

davidvnog


People also ask

What is autoloading classes in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

What is autoloading in composer?

Classmap vs. php , when composer reads that we are using autoload classmap , it is scanning all the files in the mentioned directories (in composer. json file) and create an array of namespaces and the corresponding paths. Note: adding new files requires composer dumpautoload to regenerate the mapping.

What is composer dump autoload?

Timo Reymann. Compatible with IntelliJ IDEA Ultimate, PhpStorm. Tutorials. This is a simple plugin to trigger composer to create a new autoload file when you move, create or delete PHP files in your project.

How does PHP autoload work?

Basically, when you use a class in your application, the autoloader checks if it's already loaded, and if not, the autoloader loads the necessary class into memory. So the class is loaded on the fly where it's needed—this is called autoloading.


2 Answers

PSR-4 requires the loaded files to have a namespaced class, and the namespace structure has to match the directory structure, relative to the "base directory" defined in the configuration (http://www.php-fig.org/psr/psr-4/).

So, in file /var/xxx/ModuleA/baseObjects/configClass.inc should be the class

namespace ModuleA\baseObjects;

class configClass {
    ...
}

Then in var/www/laravel/composer.json you could have

    "psr-4": {
        "App\\": "app/",
        "ModuleA\\": "../ModuleA"
    }

Which means: "directory ../ModuleA should be the root for ModuleA namespace, then follow subnamespaces by matching subdirectories from there".

like image 114
alepeino Avatar answered Sep 22 '22 06:09

alepeino


Use classmap autoload will solve this problem.

{
...
"autoload": {
        "classmap": ["ModuleA/", "ModuleB/"]
    }
}

It could be used with PSR-4

{
    ...
    "autoload": {
            "psr-4": { 
                "Acme\\": "src/Acme/"
            },
            "classmap": ["ModuleA/", "ModuleB/"]
        }
    }

Ref: https://getcomposer.org/doc/04-schema.md#classmap

like image 26
Panuwizzle Avatar answered Sep 20 '22 06:09

Panuwizzle