Using Composer Packages in XenForo 2.1+ Addons Tutorial

Using Composer Packages in XenForo 2.1+ Addons Tutorial 2.1.0

No permission to download
Q: How to upgrade my addon from XenForo v2.0 to XenForo v2.1 when I use composer packages?

Note that I believe this process is not required - the Composer code we used for XenForo 2.0 should still work on XenForo 2.1 and following this mini-tutorial will mean that your addon no longer works on XenForo 2.0

However, assuming you don't mind having a XenForo 2.1 specific addon and assuming you followed my Using Composer Packages in XenForo 2.0 Addons Tutorial when creating your addon ... then follow these steps to convert your package:

Step 1: check Listener Class

We need to work out whether the code event listener is still required.

Look at the class and function we set up to execute when the app_setup event is fired.

PHP:
<?php namespace ComposerTutorial;

use XF\App;

class Listener
{
    public static function appSetup(App $app)
    {
        Composer::autoloadNamespaces($app);
        Composer::autoloadPsr4($app);
        Composer::autoloadClassmap($app);
        Composer::autoloadFiles($app);
    }
}

If we had added additional functionality to the above function, then you should only remove the four Composer lines but leave the function, class and code event listener in place.

However, if our appSetup code is unchanged, then we no longer need it.
  1. remove the code event listener we set up to listen for the app_setup event
  2. remove the Listenter::appSetup function
  3. if we have no other functions in our Listener class, delete the entire class file too
Step 2: remove the Composer helper class

The Composer.php file we added to the root of our addon is no longer required - this code has been incorporated into the core XenForo code. We can simply remove this file completely.

Step 3: add autoload directive to addon.json

Edit your addon.json file and add the following directive to the end of it:

JSON:
"composer_autoload": "vendor/composer"

This does all the work for us, telling the XenForo core to include our addon packages in the autoload process.

Your addon should now work on XenForo 2.1 (but will no longer work on XenForo 2.0 !!).
Top Bottom