General developer forum

How to invoke a PSR-4 PHP package in a custom Moodle plugin?

 
Picture of wz z
How to invoke a PSR-4 PHP package in a custom Moodle plugin?
 

I want to invoke this PSR-4 PHP package, which is recommended to be installed by composer, because it has many dependencies and an autoload.php file. I've no idea to do call it in a custom Moodle plugin? Are there any examples?

 
Average of ratings: -
Picture of Juho Jaakkola
Re: How to invoke a PSR-4 PHP package in a custom Moodle plugin?
Particularly helpful MoodlersPlugin developers

Go to the directory that contains your plugin (for example mod/wechat/) and give the command:

composer require overtrue/wechat

It will automatically create the following files and directories:

  • composer.json
  • composer.lock
  • vendor/
The vendor directory will contain a file called autoload.php. Include that file in your plugin code, and you can start using the API provided by the package.


For example in mod/wechat/view.php:

<?php
require_once('vendor/autoload.php'); require_once('../../config.php');
// You can now access the API provided by the package:
$foo = new \Some\Example\Class($some, $parameters); $foo->doSomething();


 
Average of ratings: -
Picture of Juho Jaakkola
Re: How to invoke a PSR-4 PHP package in a custom Moodle plugin?
Particularly helpful MoodlersPlugin developers

(Oh, and in this context the correct term is "composer package" instead of "PSR-4 PHP package". PSR-4 simply refers to the way that the package is loading it's classes.)

 
Average of ratings: -