6

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.

6
  • 1
    Maybe it is too late, but are you developing your own component based on this package?
    – Pep Lainez
    Oct 11, 2016 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, 2016 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, 2016 at 14:50
  • @sifu Did you register the composer autoloader, or did you just require manually the files needed? Dec 29, 2020 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, 2020 at 4:59

1 Answer 1

5

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.

<?php 
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.

Update for Joomla 4

This is still the way I make packages available for custom code in Joomla 4. For example if I want to use intervention/image in a custom component I would use composer to install the package in a folder called composer and then add the autoloader using a system plugin where it is needed like below.

<?php 
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;

class plgSystemYaycomposer extends CMSPlugin
{
    function onAfterRoute()
    {
        $option = Factory::getApplication()->input->get('option', '',);
        if($option == "com_my_component"){
            require_once(JPATH_ROOT . '/plugins/system/yaycomposer/composer/vendor/autoload.php');
        }
    }
}

Then the package(s) are available for that component.

$intervention = new Intervention\Image\ImageManager();
2
  • Is this answer still relevant after the Joomla 4 release? Nov 2, 2023 at 8:24
  • 1
    yep. updated the answer with newer plugin code. Nov 3, 2023 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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