Using snippet in route inside plugin

We’ve got a plugin to import entries from an old site. This plugin provides some snippets to display import logs after running an import and importers are executed by a custom route.

As soon as we use kirby() inside our route, the provided snippets can no longer be found. So we tried wrapping our routes in a function as mentioned in the docs but the result is the same: the snippets from the plugin are not found. Any other snippet provided via /site/snippets is accessible though.

How can we use the $kirby object while still being able to access our snippets?


This is the code of our plugin:

<?php

include_once __DIR__ . '/Importer.php';

Kirby::plugin('hananils/kirby-importer', [
    'snippets' => [
        'hananils/importer/header' => __DIR__ . '/snippets/header.php',
        'hananils/importer/footer' => __DIR__ . '/snippets/footer.php',
        'hananils/importer/list' => __DIR__ . '/snippets/list.php',
        'hananils/importer/run' => __DIR__ . '/snippets/run.php'
    ],
    'routes' => function ($kirby) {
        return [
            [
                'pattern' => 'import',
                'action' => function () {
                    snippet('hananils/importer/list');
                }
            ],
            [
                'pattern' => 'import/(:all)',
                'action' => function ($importers) use ($kirby) {
                    $importers = explode('+', $importers);
                    $logs = [];

                    foreach ($importers as $importer) {
                        $file =
                            $kirby->root('site') .
                            '/importers/' .
                            $importer .
                            '.php';

                        if (F::exists($file)) {
                            include_once $file;

                            $name = Str::ucfirst($importer);
                            $class = new $name();
                            $logs[] = $class->run();
                        }
                    }

                    snippet('hananils/importer/run', [
                        'logs' => $logs
                    ]);
                }
            ]
        ];
    }
]);

$kirby->root('site') in the second route is the problematic line. If I remove it the snippet is found again but of course my importer doesn’t run because the URL is no longer complete.

Not sure I understand. So the first route works and finds the registered snippets?

But the second route is not about snippets, is it? But the files in a /site/importers folder are not found?

All snippets are provided by the plugin itself, so they are located as /site/plugins/importer/snippets and registered by the snippets array above.

The first route works fine, the snippet is found and the resulted HTML echoed.
The second route does not find the snippet – the reason being the call to $kirby (or kirby(), doesn’t matter). So if I remove the call to $kirby the snippet is echoed as well, but that doesn’t help because I need the Kirby object to get the path to my importers.

But this path seems wrong to me

No, the path is correct, that’s not the path for the snippets but for the files containing the import logic. The import itself works without an issue, I can dump the result of $logs to see the its success.

It’s more like the Kirby object is reset by the call to $kirby so it “forgets” about the custom snippets.

The logic to register the snippets is provided by Kirby itself:

Does this work for you?

<?php

Kirby::plugin('your/plugin', [
    'snippets' => [
        'test' => __DIR__ . '/snippets/test.php'
    ],
    'routes' => function ($kirby) {
        return [
            [
                'pattern' => 'test',
                'action' => function () use ($kirby) {
                    snippet('test', ['kirby' => $kirby]);
                }
            ]
      ];
    }
]);

Thanks, @ahmetbora, this works. I’m asking myself if this is something similar to https://github.com/getkirby/kirby/pull/2894.

Okay, I found it. Thanks for your help but it was a totally %$§@ mistake where one import script was calling $kirby::destroy() :confounded: