2017 © Pedro Peláez
 

library c-flash-message

Adding a Flash-Message function

image

paaa14/c-flash-message

Adding a Flash-Message function

  • Monday, March 21, 2016
  • by patrikalbertsson
  • Repository
  • 1 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

CFlashMessage

build:passed Scrut CodeCoverage, (*1)

This is an module for simple flash-messages. It contains in general three functions:, (*2)

setFlash('type', 'message'): Types allowed: success, notice, warning and error., (*3)

hasFlash(): Returns true if flash is set, else false., (*4)

getFlash(): Get flashMessage., (*5)

How to use CFlashMessage

1. Install via composer

Install CFlashMessage via packagist and composer. Add, (*6)

"require": {
    "php": ">=5.4",
    "paaa14/c-flash-message" : "dev-master"
},

in your composer.json., (*7)

2. Set FlashMessage-class

Set flashMessage in your base class implementing Dependency Injection / Service Locator- file. If you are using Anax-MVC, ex., (*8)

File:src/DI/CDIFactoryDefault.php, (*9)

Add $this->setShared('flashMessage', '\Anax\FlashMessage\CFlashMessage') (you maybe want to change the namespace for CFlashMessage.php), (*10)

3. Implement code in your template-file

File: theme/anax-base/index.tpl.php, (*11)

In your div#main, put code:, (*12)

<?php if($this->flashMessage->hasFlash()) : ?> 
    <?php $message = $this->flashMessage->getFlash(); ?>
    <div class="<?=$message['type']?>"><?=$message['message']?></div>
<?php endif; ?>

to see if flash has been set. If it returns true, show flashMessage., (*13)

Exemple:, (*14)

<div id='main'>
<?php if(isset($main)) echo $main?>
<?php $this->views->render('main')?>
<?php if($this->flashMessage->hasFlash()) : ?> 
    <?php $message = $this->flashMessage->getFlash(); ?>
    <div class="<?=$message['type']?>"><?=$message['message']?></div>
<?php endif; ?>
</div>

4. Add stylesheet

Copy flash.css to your css-archive and be sure that flash.css is activated in your framework., (*15)

5. Session

Be sure that Sessions is activated in your framework. If you var_dump($_SESSION) you should have an empty session. If you are using Anax-MVC you might have to start the session manually i your frontcontroller., (*16)

Put $app->session() in the top of your controller, after you assign $app, (*17)

6. Router

Make sure your framework handles routes. If you are using Anax-MVC, put $app->router->handle() in the bottom of your frontcontroller, before it's being sent to render., (*18)

7. Create routes and use FlashMessage

Exemple in Anax-MVC:, (*19)

$app->router->add('', function() use ($app) {
    $app->flashMessage->setFlash('success', "Success! Update page to make this message disappear.");
    $app->views->addString('Flash is set, click on the link to view the flash', 'main');  
    $app->views->addString("<a href='" . $app->url->create('flash') . "'>Link</a>", 'main');
});

$app->router->add('flash', function() use($app) {
    $app->views->addString('In your template-file, there is a if-statement that checks if a flash has been set. If it returns TRUE, this message is shown. Update page to remove this message.');
});

To get Anax-MVC, visit: https://github.com/mosbth/Anax-MVC, (*20)

Enjoy :), (*21)

The Versions

21/03 2016

dev-master

9999999-dev

Adding a Flash-Message function

  Sources   Download

The Requires

  • php >=5.3.0

 

18/03 2016

v1.0

1.0.0.0

Adding a Flash-Message function

  Sources   Download

The Requires

  • php >=5.3.0