Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

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

Solved by pamtbaau View solution

Started by Julien 5 years ago · 3 replies · 1509 views
5 years ago

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:

TXT
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.

5 years ago Solution

@domsson,

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.

Steps:

  • 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
last edited 02/03/21 by pamtbaau
5 years ago

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?

5 years ago

@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

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 49 1 week ago
Plugins · by Xavier, 4 weeks ago
2 57 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1184 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 50 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 75 2 months ago