euromark 11 years ago
parent
commit
194b5e4e0c

+ 38 - 0
docs/Component/Flash.md

@@ -0,0 +1,38 @@
+# Flash Component
+
+An enhanced FlashComponent capable of
+- Stackable messages for each type
+- Persistent (across requests) and transient messages
+- Inject it into the headers as `X-Ajax-Flashmessage` for REST/AJAX requests
+
+## Configs
+
+
+## Usage
+Attach it to your controllers in `initialize()` like so:
+```php
+$this->loadComponent('Tools.Flash');
+```
+
+Also add the helper for it:
+```php
+public $helpers = array('Tools.Flash');
+```
+
+In your layouts, you don't need to change the `$this->Flash->render()` call, as the syntax for this helper is the same.
+
+## Basic Example
+```php
+// Inside an action
+$this->Flash->message('Yeah it works.', 'success');
+$this->Flash->message('Careful.', 'warning');
+$this->Flash->message('O o.', 'error');
+```
+
+## You can also use the new syntactic sugar:
+```php
+// Inside an action
+$this->Flash->success('Yeah it works.');
+$this->Flash->warning('Careful.');
+$this->Flash->error('O o.');
+```

+ 12 - 1
src/View/Helper/FlashHelper.php

@@ -18,12 +18,23 @@ class FlashHelper extends Helper {
 	/**
 	 * Display all flash messages.
 	 *
+	 * @param array $types Types to output. Defaults to all if none are specified.
+	 * @return string HTML
+	 * @deprecated Use render() instead
+	 */
+	public function flash(array $types = array()) {
+		return $this->render($types);
+	}
+
+	/**
+	 * Display all flash messages.
+	 *
 	 * TODO: export div wrapping method (for static messaging on a page)
 	 *
 	 * @param array $types Types to output. Defaults to all if none are specified.
 	 * @return string HTML
 	 */
-	public function flash(array $types = array()) {
+	public function render(array $types = array()) {
 		// Get the messages from the session
 		$messages = (array)$this->Session->read('messages');
 		$cMessages = (array)Configure::read('messages');

+ 7 - 7
tests/TestCase/View/Helper/FlashHelperTest.php

@@ -36,14 +36,14 @@ class FlashHelperTest extends TestCase {
 	}
 
 	/**
-	 * FlashHelperTest::testFlash()
+	 * FlashHelperTest::testRender()
 	 *
 	 * @return void
 	 */
-	public function testFlash() {
+	public function testRender() {
 		$this->Flash->addTransientMessage(h('Foo & bar'), 'success');
 
-		$result = $this->Flash->flash();
+		$result = $this->Flash->render();
 		$expected = '<div class="flash-messages flashMessages"><div class="message success">Foo &amp; bar</div></div>';
 		$this->assertEquals($expected, $result);
 
@@ -53,7 +53,7 @@ class FlashHelperTest extends TestCase {
 		$this->Flash->addTransientMessage('I am also some info');
 		$this->Flash->addTransientMessage('I am sth custom', 'custom');
 
-		$result = $this->Flash->flash();
+		$result = $this->Flash->render();
 		$this->assertTextContains('message error', $result);
 		$this->assertTextContains('message warning', $result);
 		$this->assertTextContains('message info', $result);
@@ -76,15 +76,15 @@ class FlashHelperTest extends TestCase {
 		$this->Flash->addTransientMessage('I am also some info');
 		$this->Flash->addTransientMessage('I am sth custom', 'custom');
 
-		$result = $this->Flash->flash(array('warning', 'error'));
+		$result = $this->Flash->render(array('warning', 'error'));
 		$expected = '<div class="flash-messages flashMessages"><div class="message warning">I am a warning</div><div class="message error">I am an error</div></div>';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Flash->flash(array('info'));
+		$result = $this->Flash->render(array('info'));
 		$expected = '<div class="flash-messages flashMessages"><div class="message info">I am some info</div><div class="message info">I am also some info</div></div>';
 		$this->assertEquals($expected, $result);
 
-		$result = $this->Flash->flash();
+		$result = $this->Flash->render();
 		$expected = '<div class="flash-messages flashMessages"><div class="message custom">I am sth custom</div></div>';
 		$this->assertEquals($expected, $result);
 	}