How to use a third library?

Hi
How do I use a third libraray like PhpSpreadsheet?
I copied the pluginfiles to here:
image

And how can I call the Plugin now?
I tried this, but the Class cannot be found:
$reader = new PhpSpreadsheet\Reader\Xlsx();

What is the Syntax to access functions of a thirdparty plugin?

All plugins in Kirby work the same way as explained in the docs:

You have to create a plugin folder with an index.php inside. Then in your index.php you can load your dependencies, either via composer autoload, loading manually with require or with Kirby’s load() function.

Please read the documentation about plugins.

You have to create a plugin folder with an index.php inside. Then in your index.php you can load your dependencies, either via composer autoload, loading manually with require or with Kirby’s load() function.

You mean in the PhpSpreadsheet-folder I have to add a index.php?
And then adding this to index.php?

<?php
require 'vendor/autoload.php';
use PhpSpreadsheet\Spreadsheet;
?>

And then I can use it for example in a model like this?

$reader = new PhpSpreadsheet\Spreadsheet\Reader\Xlsx();

It does not work.

To the documentation:
Sorry, but in my opinion, the documentation is for php-experts who already know the big picture.
For me it always feels like important explanations are missing because it is assumed that you already know it - I’m completely lost there. Or maybe coding in general is just an overkill for my cognitive possibilities :grimacing:

No, the spreadsheet folder goes into the plugin folder next to the index.php,

The guide has a whole chapter about plugin development for all the different options: Plugin Basics | Kirby CMS

@pixelijn
Thanks again for your patience.
I found out a way to install a thirdparty plugin.

For all the dumpheads like me out there :slight_smile:
Here is the **step by step instruction to install (with composer) and use the phpspreadsheed library **:

  1. Create a folder called “phpspreadsheet” in \site\plugins (create “plugins” if it does not exist yet)

  2. In the terminal switch into \site\plugins\phpspreadsheet

  3. Run composer require phpoffice/phpspreadsheet

  4. Choose “no” if the terminal asks you to use composer.json from the root directory. Else you will end up in a dependency-mess!

  5. In \site\plugins\phpspreadsheet create the file index.php

  6. In this index.php write this
    <?php require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet;?>

  7. Then from a model you can read the data from an excelfile like this:
    $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
    $spreadsheet = $reader->load($this->root() . “/offersdata.xlsx”);

Full example witch just returns the number of rows (yes, very useful ;-)):
(Continue the tutorial at the bottom if you wanna do more usefull things).

Sources:

2 Likes