rtheunissen
rtheunissen

Reputation: 7435

Custom autoloader for project vs Composer PSR-4

I'm currently working on a small framework which has the following directory structure:

/
-- app
-- vendor
   -- framework-vendor
      -- framework-package
         -- src

Currently what I have in my framework's composer.json are two PSR-4 instances:

"autoload": {
    "psr-4" : {
        "FrameworkName\\" : "src/",
        "": "../../../app"
    }
}

The goal of this is that the actual project located at app can use classes in the framework using \FrameworkName\foo\bar\Class but also its own classes using \foo\bar\Class. The only limitation to this is the relative depth between the vendor directory and the app directory.

What's the best way to do this? Just include a second autoloader or enforce this directory structure and let Composer take care of generating the autoloader?

Upvotes: 0

Views: 712

Answers (1)

rtheunissen
rtheunissen

Reputation: 7435

The solution to not force relative path depth is to use the autoloader returned by Composer like this:

$loader = require 'vendor/autoload.php';
$loader->add('', __DIR__ . '/app/');

Upvotes: 1

Related Questions