How to configure a composer package to be globally installed? How to configure a composer package to be globally installed? php php

How to configure a composer package to be globally installed?


Targeting vendor/autoload.php is generally not a good idea and only works if you run the script from the correct directory. The following should serve you better:

require_once __DIR__.'/../vendor/autoload.php';

However, this still might be an issue if your application is installed as a dependency. In that case, you might need something more substantial:

if (    (!$classLoader = includeIfExists(__DIR__.'/../vendor/autoload.php')) &&    (!$classLoader = includeIfExists(__DIR__.'/../../../autoload.php'))) {    echo 'You must set up the project dependencies, run the following commands:'.PHP_EOL.        'curl -sS https://getcomposer.org/installer | php'.PHP_EOL.        'php composer.phar install'.PHP_EOL;    exit(1);}

This first looks for the autoloader in the location you would expect it to be if you are working directly on your application. If that does not exist, it looks where the autoloader would be if your application is installed as a dependency.