Plugins: vendor/autoload.php - it broke; why and best practice?

I’ve got a self-written plugin that came about by going through the Plugin Tutorial. One of the snippets of code that the base plugin structure already provides is this:

public function autoload(): ClassLoader
    return require __DIR__ . '/vendor/autoload.php';

Everything worked - until today, when I decided to do some content editing, but both the backend as well as frontend presented me with an error (edited server paths and plugin name to make it more generic):

require(): Failed opening required '/path/to/the/project/user/plugins/plugin-name/vendor/autoload.php' (include_path='.:/usr/lib/php7.4')

I checked, there is no vendor directory in my plugin. Should there be one? If so, what should the contents of the autoload.php look like? Checking other plugins, like grav-plugin-email, it seems like this file is auto-generated. But through what process? And why did everything work until just now, then suddenly broke?

Disclaimer: I have no idea about what composer is or does.


Composer is bit like ‘npm’ for Nodejs.

Yes, if you followed the steps in the tutorial correctly, there should be a ‘/vendor’ folder.

Autoloading will allow you to easily reference classes provided by your own plugin (in ‘/classes’) or from vendor packages (’/vendor/<package>/…’)

What went wrong:
While following the steps in the tutorial you probably skipped some important steps.

During step 2, from the tutorial you are referring to…

  • The cli command $ bin/plugin devtools new-plugin finishes with the note:

    Make sure to run composer update to initialize the autoload scripts.

    Also a following note indicates you have to run composer update

    At this point you need to run composer update in the newly created plugin folder.


  • Create plugin: $ bin/plugin devtools new-plugin
  • Change directory to the newly created plugin: $ cd user/plugins/<your plugin>
  • Let composer create the ‘/vendor’ folder and required autoload scripts: $ composer update
1 Like

I see, thank you. I’m still puzzled why it worked up until now and suddenly threw an error, but this seems like the solution. I still have to try it, as I’m currently struggling with another issue that needs to be resolved first.

One follow-up question though: If I know, for a fact, that my plugin does not - and most likely never will - have any dependencies, would it be save to entirely remove the autoload() function and skip the composer steps?

@domsson, After upgrading to 1.7, the command $ bin/plugin devtools new-plugin creates a plugin containing new recommendations. See the 1.7 Upgrade docs and its recommendations.

Yes, you can remove the autoload() function if you have no dependencies and have no own classes and in that case skip composer update

Grav first checks if Plugin::autoload() exists before calling it.

1 Like