Hi,
I’m generating “lessons” by clicking a controlpanel button (new in v5), executing my model. The concept works, my model generates the lessons. But I’d like to show a notification message saying “lessons created”! Whatever I try (with my AI friends (
I cannot make the message appear in the control panel.
below my config.php with the code.
Any help is welcome…
<?php
// site/config/config.php
//
// UPDATED: Using a full Response object for the redirect, which is more reliable.
?>
<?php
return [
'debug' => true,
'routes' => [
[
'pattern' => '(:all)/generate-lessons',
'action' => function (string $slug) {
$kirby = kirby();
$page = page($slug);
if (!$page || $page->intendedTemplate()->name() !== 'lessongroup') {
return false;
}
if ($page->lessonsGenerated()->isTrue()) {
$message = 'Lessons have already been generated. To regenerate, turn off the toggle and save first.';
$kirby->session()->set('kirby.info', $message);
return Kirby\Http\Response::redirect($page->panel()->url());
}
try {
// Step 1: Generate lessons and get the count.
$count = $page->generateLessons();
// Step 2: Manually update the page toggle. This is the action.
$page->update(['lessonsGenerated' => true]);
// Step 3: Set the success message in the session.
$message = "{$count} lessons were successfully generated for '{$page->description()}'";
$kirby->session()->set('kirby.success', $message);
// Step 4: Redirect back to the panel using the Response object.
return Kirby\Http\Response::redirect($page->panel()->url());
} catch (Exception $e) {
$kirby->session()->set('kirby.error', $e->getMessage());
return Kirby\Http\Response::redirect($page->panel()->url());
}
}
]
]
];