PHP code example of markocupic / contao-flash-message

1. Go to this page and download the library: Download markocupic/contao-flash-message library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

markocupic / contao-flash-message example snippets




declare(strict_types=1);

namespace App\Controller;

use Markocupic\ContaoFlashMessage\FlashMessage\MessageInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment as Twig;

#[Route('/shop/shop_checkout', name: ShopCheckoutController::class, defaults: ['_scope' => 'frontend', '_token_check' => true])]
class ShopCheckoutController extends AbstractController
{
    public function __construct(
        #[Autowire(service: 'markocupic_contao_flash_message.flash_message.default')]
        private readonly MessageInterface $message,
        private readonly Twig $twig,
    ) {
    }

    public function __invoke(): Response
    {
        $this->message->addError('This is my first error message.');
        $this->message->addInfo('This is fy first info message.');
        $this->message->addSuccess('This is my first success message.');

        return new Response($this->twig->render(
            'App/Shop/checkout.html.twig',
            [
                'messages' => $this->message->render(), // The messages are rendered as an HTML string.
            ],
        ));
    }
}



namespace App\FlashMessage\Checkout;

use Markocupic\ContaoFlashMessage\FlashMessage\AbstractMessage;
use Markocupic\ContaoFlashMessage\FlashMessage\MessageInterface;
use Markocupic\ContaoFlashMessage\FlashMessage\Trait\ErrorTrait;
use Markocupic\ContaoFlashMessage\FlashMessage\Trait\InfoTrait;
use Markocupic\ContaoFlashMessage\FlashMessage\Trait\SuccessTrait;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('mc.flash_message.message_handler', attributes: ['messageId' => 'shop_checkout'])]
class Message extends AbstractMessage implements MessageInterface
{
    use ErrorTrait;
    use InfoTrait;
    use SuccessTrait;

    /**
     * This method is mandatory,
     * it returns all available message types!
     */
    public function getTypes(): array
    {
        return [self::TYPE_ERROR, self::TYPE_INFO, self::TYPE_SUCCESS];
    }
}




declare(strict_types=1);

namespace App\Controller;

use App\FlashMessage\Checkout\Message;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment as Twig;

#[Route('/shop/shop_checkout', name: ShopCheckoutController::class, defaults: ['_scope' => 'frontend', '_token_check' => true])]
class ShopCheckoutController extends AbstractController
{
    public function __construct(
        private readonly Message $message,
        private readonly Twig $twig,
    ) {
    }

    public function __invoke(): Response
    {
        $this->message->addError('This is my first error message.');
        $this->message->addInfo('This is fy first info message.');
        $this->message->addSuccess('This is my first success message.');

        return new Response($this->twig->render(
            'App/Shop/checkout.html.twig',
            [
                'messages' => $this->message->render(), // The messages are rendered as an HTML string.
            ],
        ));
    }
}