You are here: start » en » Internal Documentation » DokuWiki extensions (WvdS) » Core » Quickstart: register the first service and read it back

Quickstart: register the first service and read it back

The shortest way from “the plugin is installed” to “a service of your own is in the registry and is found from elsewhere”. Three steps, about fifteen minutes. This page is for developers of your own plugins.

Prerequisites

  • wkcore is installed and enabled in the Extension Manager.
  • A plugin of your own with an action component (action.php), or a place to add one.
  • PHP 8.2 or later.
  • For step 3 (the check): you may edit conf/local.php.

Step 1: hook the registration event

WKCORE_REGISTER_SERVICES is the only point at which the registry expects to be written to.

use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
 
class action_plugin_myplugin extends ActionPlugin
{
    public function register(EventHandler $controller)
    {
        $controller->register_hook('WKCORE_REGISTER_SERVICES', 'BEFORE', $this, 'onRegisterServices');
    }
 
    public function onRegisterServices(Event $event)
    {
        $registry = $event->data;
        $registry->register('myplugin.greeter', new Greeter());
    }
}

Choose an id that starts with your plugin's own prefix. Ids are a flat namespace with no collision check; the prefix is the only thing keeping two plugins apart.

If the service is expensive to build, or needed on few requests, register a factory instead. It runs on the first get() and never again:

$registry->registerFactory('myplugin.report', static function () {
    return new ExpensiveReport();
});

Step 2: consume it from elsewhere

Through DokuWiki's plugin loader — not by importing wkcore classes:

$core = plugin_load('helper', 'wkcore');
if (is_object($core) && method_exists($core, 'get')) {
    $greeter = $core->get('myplugin.greeter');
    if ($greeter !== null) {
        echo $greeter->greet();
    }
}

The two guards are the house convention and they earn their keep: your plugin keeps loading when wkcore is absent, and keeps working when a service it expected was never registered. Neither situation is worth a fatal.

Step 3: look at what is registered

The diagnostics view is off unless DokuWiki's allowdebug setting is on — it then reports only wkcore diagnostics require $conf['allowdebug'] = 1. Set in conf/local.php:
$conf['allowdebug'] = 1;

and take the line out again after the check. The view exposes internals of the installation.

Then open /doku.php?do=wkcore_diag.

Check the result

The view lists every registered id, whether it is a value or a factory, and for factories whether they have been resolved on this request.

  • myplugin.greeter is there as a value.
  • myplugin.report is there as an unresolved factory. Call get('myplugin.report') once and reload: now it is resolved.

That is exactly what the view is for. A mistyped id is not an error anywhere — it is a null from get(). Here you see what it is really called.

Next step

  • wkstorage — the first service you are likely to consume.
  • wkcore (German) — the package page: every registry method, the three events and their stability.
en/wiki/dwe/wkcore/quickstart.txt · Last modified: by 0.0.0.0