I have the below dependency injection configured.
I am using Version 6 | Session (odan.github.io) for the session management and flash messages.
$containerBuilder->addDefinitions([
Twig::class => function (ContainerInterface $c): Twig {
$twig = Twig::create($path, [
'cache' => CHUM_CACHE_PATH,
'autoescape' => false,
'debug' => true
]);
$flash = $c->get(SessionInterface::class)->getFlash();
$twig->getEnvironment()->addGlobal('flash', $flash);
return $twig;
},
SessionManagerInterface::class => function (ContainerInterface $c): SessionInterface {
return $c->get(SessionInterface::class);
},
SessionInterface::class => function (ContainerInterface $c) {
$options = [
'name' => 'app',
'lifetime' => 7200,
'path' => null,
'domain' => null,
'secure' => false,
'httponly' => true,
'cache_limiter' => 'nocache',
];
return new PhpSession($options);
},
]);
$app->add(SessionStartMiddleware::class);
I am setting the error message when there is a form validation issue and redirect the page to the same url again to show the message.
class InstallController extends BaseController
{
public function index(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$form = $this->createForm(InstallAppConfigType::class);
$req = Request::createFromGlobals();
$this->session->getFlash()->set("error", array("Invalid Root Path"));
$form->handleRequest($req);
if ($form->isSubmitted() && $form->isValid()) {
$app = $form->getData();
if (!is_dir($app->getRootPath())) {
// SETTING FLASH MESSAGE IN CASE OF ANY VALIDATION ISSUE
$this->session->getFlash()->set("error", array("Invalid Root Path", "Check Permissions"));
return $this->redirectByName($response, "install"); // redirect to same page to show error
}
return $this->redirectByName($response, "install.db");
}
return $this->render(
$request,
$response,
'install.start.twig',
array('form' => $form->createView())
);
}
}
Below is the twig template.
<span>Test : {{ flash.get('error') | first }}</span>
{% for message in flash.get('error') %}
<div class="alert alert-error shadow-lg">
<span>{{ message }}</span>
</div>
{% endfor %}
But unfortunately, I do not see any message shown. What I am missing here.