Create Plugin - where to store vendor and how to autoload // Custom Kirbytag

Hello there,

i want to create a plugin for kirby to communicate with an API.
With composer i can load all depencies, but i am unsure where to put the composer require line.
I think it’s best practice to put it into my plugin root’s composer.json.
The folder “vendor” is created and all files are there.

I need to create a new instance from a class within the vendor path, but kirby can’t find the Class.
Do i need to setup any kind of autoload, so the Classes in Vendor are found?

I did:
include ‘vendor/autoload.php’;
but the classes loaded there are not found.

I know the documentation of Kirby’s plugin but i can’t find resources referring to autoload vendor classes.

Thank you all!

Maybe an example helps: https://github.com/texnixe/kirby-mobile-detect

require __DIR__ . '/vendor/autoload.php';

Edit: See also here for using the imported classes in your own namespace: https://github.com/pedroborges/kirby-autogit/blob/master/lib/autogit.php

1 Like

Thank you @texnixe this will help me much.
Have a good sunny friday.

1 Like

Quick Update: @texnixe hint helped me that much… Kirby and the API are communicating.

Yes! :slight_smile:

Sorry to warm up this thread again, but i think it fits best.

My Question:
I want to create a new custom Kirby Tag where i call a method in a class of my new plugin.
But… Kirby can’t find the Class.

I am working in a template and call it like “classname()” - but that wont work on kirbytag.

Is there something i want to achieve whats not supposed to work?

Thank you!

Is that a method of a class you created yourself? Or a method of a class of the imported library? Have you instantiated an object of that class?

In the template i call:
$strava = strava();
The strava() function is inside the plugins/strava/strava.php and calls a new Instance.

The result is:

Error
Call to undefined function strava()

I orientated on the Instragam Plugin i got on the Plugin site.

Could you post your relevant code from the strava.php please?

plugins/strava/strava.php

<?php
require __DIR__ . '/vendor/autoload.php';
use Strava\API\Exception;
use Strava\API\Service\REST;
use Strava\API\Client;

function strava(){
	return new strava();
}

class strava {
	var $connection;
    var $segment;
    var $athlete;
    var $club;
    var $token;

    function __construct($_cache = true) {
        $this->token = 'xxx';

	    // Create cache directory if it doesn't exist yet
        if ($_cache) {
            dir::make(c::get('root.cache') . '/strava');
        }
        $this->connection = 'connection';
    }

    function getClient() {
        $adapter = new Pest('https://www.strava.com/api/v3');
        $service = new REST($this->token, $adapter);  // Define your user token here.
        $client = new Client($service);
        return $client;
    }

    function athlete($id = null){
        try {
            $this->athlete = $this->getClient()->getAthlete($id);
            return $this->athlete;
        } catch(Exception $e) {
            print $e->getMessage();
        }
    }

    function segment($id) {
        try {
            $this->segment = $this->getClient()->getSegment($id);
            return $this->segment;
        } catch(Exception $e) {
            print $e->getMessage();
        }
    }
}

It probably has to do with load order. Do you register the tag in a plugin or does the tag live in the tags folder?

It is in the site/tags folder, as a custom tag. Shall i put it to the plugin folder then?

Hm, I did a quick test and didn’t have a problem with the tag in the tags folder. Could you post your tag code, please?

This is my tag (just an example yet):

<?php
$strava = strava();
kirbytext::$tags['strava'] = array(
'html' => function($tag) {
    return '<a href="">'.$tag->attr('strava').'</a>';
}
);

The error is thrown on line 2: $strava = strava();

Ah, I see, your code must be inside the tag array, not outside of it.

kirbytext::$tags['strava'] = array(
  'html' => function($tag) {
    return dump(strava());
  }
);
1 Like

Yes! That was it… thank you @texnixe!

If i want to keep the custom tags inside the plugin, which makes more sense in my opinion…
how could it be possible to load the new kirbytag within plugins/strava/tags/?

Currently it’s in “tags”.

You can register it in your plugin like this:


$kirby->set('tag', 'wikipedia', array(
  'html' => function($tag) {
    return '<a href="http://wikipedia.org">Wikipedia</a>';
  }
));

The whole tag code would then live in your plugin file.

And yes, if it is part of the plugin, that definitely makes sense.

Super… that works now.
Thank you!

But… would it be possible to create a folder “tags” inside the plugin folder? So the Tags are organized like the customs tags in “site/tags”. Or am i wrong?

According to the docs, that doesn’t seem to be possible and I remember that I tried that once without success. But I’m not 100% sure.

I noticed you changed the category back to plugins. Please note that the plugins category is rather for “advertising” plugins you created, posting plugin wishes and discussing plugin ideas.