PHP code example of arturdoruch / flash-message-bundle

1. Go to this page and download the library: Download arturdoruch/flash-message-bundle 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/ */

    

arturdoruch / flash-message-bundle example snippets


// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new ArturDoruch\FlashMessageBundle\ArturDoruchFlashMessageBundle(),
    );
}

public function indexAction()
{
    $flash = $this->get('ad_flash_message');
    ...
}

/**
 * Translates custom type message and sets it into session flash bag. Overrides previous message if was set.
 *
 * @param string      $type        Can be any string describing action status. For types: "success", "error", "notice"
 *                                 use dedicated methods "setSuccess", "setError", "setNotice".
 *
 * @param string|null $message     Message text. Given text is always translated by "Symfony\Component\Translation\Translation"
 *                                 with "message" domain as default (of course if exists any translations for given string).
 *                                 If $message is null then will be dynamically creates as "translationId" based
 *                                 on called controller action name in convention: "company_bundle_controller.action.$type".
 *                                 For example, if we call this method (and $message is null) in controller
 *                                 "App\DemoBundle\Controller\ProductController::createAction"
 *                                 then will be generated this "translationId" value: "app_demo_project.create.$type".
 *
 * @param array       $parameters  Parameters for translation message.
 * @param string|null $domain      Translation domain. As default is "messages".
 */
public function set($type, $message = null, array $parameters = array(), $domain = null) {}

/**
 * Adds and translates custom type flash message.
 */
public function add($type, $message = null, array $parameters = array(), $domain = null) {}

/**
 * Gets custom type translated flash message.
 * This method not adds message into session flash bug.
 * Just creates, translates and returns it.
 */
public function get($type, $message = null, array $parameters = array(), $domain = null) {}

/**
 * Available methods sets, adds or gets messages with concrete types: "Success", "Error", "Notice".
 */
public function setSuccess($message = null, array $parameters = array(), $domain = null) {}
public function setError($message = null, array $parameters = array(), $domain = null) {}
public function setNotice($message = null, array $parameters = array(), $domain = null) {}

public function addSuccess($message = null, array $parameters = array(), $domain = null) {}
public function adsError($message = null, array $parameters = array(), $domain = null) {}
public function addNotice($message = null, array $parameters = array(), $domain = null) {}

public function getSuccess($message = null, array $parameters = array(), $domain = null) {}
public function getError($message = null, array $parameters = array(), $domain = null) {}
public function getNotice($message = null, array $parameters = array(), $domain = null) {}

/**
 * Adds and translates flash message for CRUD action.
 * Generates "translationId" based on given parameters values and/or
 * dynamically generated based on $entity value and called controller name.
 * Generated "translationId" has format "crud.action.type".
 * All CRUD messages are translated with "crudMessages" domain.
 * See 'Resources/translations/crudMessages.en.yml'.
 *
 * @param string $type         Can be any string describing action status. For types: "success", "error", "notice"
 *                             use dedicated methods "addCrudSuccess", "addCrudError", "addCrudNotice".
 *
 * @param string $entity       Persistence entity object or entity name. Is used as parameter %entity%
 *                             in translation files.
 *                             For more clarify see "Resources/translations/crudMessages.en.yml" file.
 *
 * @param null|string $item    Single entity object name. Is used as parameter %item% in translation files.
 *                             For more clarify see "Resources/translations/crudMessages.en.yml" file.
 *
 *                             If $item is null and $entity is object then
 *                             will attempt to call methods getName() on $entity object "$entity->getName()".
 *                             If method exists then $item will be filled by the returned value.
 *
 * @param null|string $action  This parameter is used for generate "translationId".
 *                             If null then $action is generated based on called controller action name.
 *                             For example if called controller is     "App\DemoBundle\Controller\ProductController::createAction"
 *                             $action will be "create".
 */
public function addCrud($type, $entity, $item = null, $action = null) {}

/**
 * Gets custom type translated flash message for CRUD action.
 * This method not adds message into session flash bug.
 * Just creates, translates and returns it.
 */
public function getCrud($type, $entity, $item = null, $action = null) {}

/**
 * Other available methods adds or gets CRUD messages with concrete types: "Success", "Error", "Notice".
 */
public function addCrudSuccess($entity, $item = null, $action = null) {}
public function addCrudNotice($entity, $item = null, $action = null) {}
public function addCrudError($entity, $item = null, $action = null) {}

public function getCrudSuccess($entity, $item = null, $action = null) {}
public function getCrudNotice($entity, $item = null, $action = null) {}
public function getCrudError($entity, $item = null, $action = null) {}

public function indexAction()
{
    ...
    $flash = $this->get('ad_flash_message');

    // Set flash messages
    $flash->setSuccess();
    $flash->setError();
    $flash->set('customType', 'Some flash message');

    // Add another message for "success" type array collection.
    $flash->addSuccess('Another success message');
}

public function sendEmailAction()
{
    ...
    $flash = $this->get('ad_flash_message');

    // Set flash messages with custom domain
    // Email was successfully send.
    $parameters = array(
        '%user%' => 'John Doe'
    );
    $flash->setSuccess(null, $parameters, 'emailMessages');
    
    // Failure email sending.
    $flash->setError(null, array(), 'emailMessages');

    // Get flash message
    return new Response($flash->getSuccess(null, $parameters, 'emailMessages'));
}

public function updateAction(Product $product, Request $request)
{
    ...
    $product->setName('Framework');
    
    // Create and valid form. If form is valid save entity and set flash message.
    
    $flash = $this->get('ad_flash_message');
    // Message will be "Product Framework has been updated."
    $flash->addCrudSuccess($product);
    
    // For this message in translation file "crudMessages" must be defined
    // new key 'crud.customaction.success'.
    $flash->addCrudSuccess('Product2', 'Bundle', 'customAction');
    // Add Crud message with custom type.
    $flash->add('customType', 'Product', 'Github');
    
    // Get message
    $updateSuccessMsg = $flash->getCrudSuccess($product);
}