interitty / flash-message-control
A visual component for applications using the Nette framework, allowing them to work easily and anywhere in a unified way with flash messages.
Requires
- php: ~8.3
- dg/composer-cleaner: ~2.2
- interitty/application: ~1.0
Requires (Dev)
- interitty/code-checker: ~1.0
- interitty/phpunit: ~1.0
README
A visual component for applications using the Nette framework, allowing them to work easily and anywhere in a unified way with flash messages.
Requirements
- PHP >= 8.3
Installation
The best way to install interitty/flash-message-control is using Composer:
composer require interitty/flash-message-control
Register in config.neon
Then register the component in the Nette config file:
# app/config/config.neon
services:
FlashMessageControlFactory:
implement: Interitty\FlashMessageControl\FlashMessageControlFactoryInterface
Register in Presenter or Control
Any component or presenter that needs to work with a flash messages can then add this component.
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\FlashMessageControl\FlashMessageControl;
use Interitty\FlashMessageControl\FlashMessageControlFactoryInterface;
use Interitty\FlashMessageControl\FlashMessageControlHelperTrait;
use Nette\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
use FlashMessageControlHelperTrait;
protected FlashMessageControlFactoryInterface $flashMessageControlFactory;
public function injectFlashMessageControlFactory(FlashMessageControlFactoryInterface $flashMessageControlFactory): void
{
$this->flashMessageControlFactory = $flashMessageControlFactory;
}
protected function createComponentFlashMessage(string $name = 'flashMessage'): FlashMessageControl
{
$component = $this->flashMessageControlFactory->create($this, $name);
return $component;
}
}
ℹ️ Simplified registration
If the extension
interitty/component-model
is used in the application, the registration in Presenter is much easier.><?php >declare(strict_types=1); >namespace App\Presenters; >use Interitty\ComponentModel\ComponentLocatorTrait; >use Interitty\FlashMessageControl\FlashMessageControlHelperTrait; >use Nette\Application\UI\Presenter; >abstract class BasePresenter extends Presenter >{ use ComponentLocatorTrait; use FlashMessageControlHelperTrait; >}
Register in latte template
Once the component is registered, you can easily render it within the latte template.
{control flashMessage}
Usage
Thanks to the used FlashMessageControlHelperTrait
auxiliary functions are available for adding a flash message of the appropriate type.
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\FlashMessageControl\FlashMessageControlFactory;
use Interitty\FlashMessageControl\FlashMessageControlHelperTrait;
use Nette\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
use FlashMessageControlHelperTrait;
public function renderDefault()
{
$this->flashMessageSuccess('The :control was succesfully registered.', ['control' => FlashMessageControlFactory::class]);
}
}
It is also possible to use the otherwise standard flashMessage
method,
which has been extended with a third parameter to pass additional parameters that will be inserted after translation
in place of their :placeholders
.
All these methods return an Interitty\FlashMessageControl\FlashMessage
object that has a fluent interface for possible further settings.
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\FlashMessageControl\FlashMessage;
use Interitty\FlashMessageControl\FlashMessageControlHelperTrait;
use Nette\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
use FlashMessageControlHelperTrait;
public function renderDefault()
{
$this->flashMessage()
->setMessage('The Interitty flash messages are :status!')
->setType(FlashMessage::TYPE_SUCCESS)
->addData('status', 'awesome');
}
}
Custom template
By default, the component uses a template that renders individual flash messages as separate div
blocks with the appropriate css class settings.
However, it can certainly be useful to set a custom rendering method. This can easily be done by setting the path to the custom template, for example directly in the Nette config file.
# app/config/config.neon
services:
FlashMessageControlFactory:
implement: Interitty\FlashMessageControl\FlashMessageControlFactoryInterface
setup:
- setTemplateFile(%appDir%/Controls/FlashMessage/FlashMessageControlCustom.latte)