I am trying to add a Composer Package and make it globally available but I do not want to modify core composer.json for obvious reasons.

Is the only way to do this having a composer.json either on the Joomla root (or outside joomla root) and including the autoloader either in the index.php or defines.php? Or the current best practice to include the packages in each of the extensions as required?

The package I am trying to include is https://github.com/nette/forms and make it globally available so that I can use it for front end forms.

  • 1
    Maybe it is too late, but are you developing your own component based on this package?
    – Pep Lainez
    Oct 11 '16 at 14:12
  • @PepLainez yes. That was what I was doing. In the end. I just stored it in Joomla Root folder which is not great but worked for me for the time being.
    – sifu
    Oct 13 '16 at 20:57
  • What obvious reasons do you have? For me, it's obvious that you need to include your package in the composer.json to install it... Dec 23 '16 at 14:50
  • @sifu Did you register the composer autoloader, or did you just require manually the files needed? Dec 29 '20 at 12:56
  • @OlleHärstedt I used the composer autoloader in the component like this require JPATH_SITE . '/../vendor/autoload.php';. The composer.json file and the vendor folder were on outside Joomla root path (so one level above the index.php for Joomla).
    – sifu
    Dec 30 '20 at 4:59

I suspect that modifying Joomla's root composer.json file will come with Joomla 4. In the meantime, I think the best way to use composer packages is to include a composer.phar in a custom system plugin and then call the autoloader on the onAfterRoute event. Then it's globally available or if you want to only load the autoloader for 1 component for example you could pull the option value from the jinput and just wrap the require statement in an if. WARNING: check the Joomla library folder before including extra packages and don't install any packages that Joomla already ships with because Joomla's autoloader and your autoloader cannot co-ordinate different versions of packages and you may break everything!!! That being said that probably wont ever happen or cause any problems.

defined('_JEXEC') or die('Restricted access');

class plgSystemYayComposer extends JPlugin
    function onAfterRoute()
        require_once(JPATH_ROOT . '/plugins/system/yaycomposer/composer/vendor/autoload.php');

Obviously this technique is not applicable to development for distribution of components etc. You should probably also remove the phar file rather than leaving it in situ, and/or block all web access to the directory with an htaccess file but, y'know, I'm not your dad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.