Show messages in the Back Office

Edit on GitHub

This document describes how to show a message in the Back Office.

In the controller, you can use these shortcut methods to show a user message in the Back Office. The messages are translated later when they are rendered.

<?php
class IndexController extends AbstractController
{
    public function indexAction()
    {
        $this->addSuccessMessage($message);

        $this->addInfoMessage($message);

        $this->addErrorMessage($message);
    }
}

Show message from the Business layer

To show a message from a model, follow these steps:

  1. Declare this dependency in the module’s dependency provider:
class MyBundleDependencyProvider extends AbstractBundleDependencyProvider
{
    /**
     * @var string
     */
    public const FACADE_MESSENGER = 'messages';

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     *
     * @return \Spryker\Zed\Kernel\Container
     */
    public function provideBusinessLayerDependencies(Container $container)
    {
        $container->set(static::FACADE_MESSENGER, function (Container $container) {
            return $container->getLocator()->messenger()->facade();
        });

        return $container;
    }
}
  1. Access it from the business factory and inject it into your model:
<?php
class MyBundleBusinessFactory extends AbstractBusinessFactory
{
    public function createAnyModel()
    {
        return new AnyModel(
            $this->getFlashMessengerFacade()
        );
    }

    protected function getFlashMessengerFacade()
    {
        return $this->getProvidedDependency(MyBundleDependencyProvider::FACADE_MESSENGER);
    }
}
  1. Use it in your model:
<?php
class AnyModel
{
    protected $flashMessengerFacade;


    public function __construct(FlashMessengerFacade $flashMessengerFacade)
    {
        $this->flashMessengerFacade = $flashMessengerFacade;
    }

    public function doSomething()
    {
        $this->flashMessengerFacade->addInfoMessage('Hello world!');
    }
}