Creating Your Own Flash Scope
The ColdBox Flash capabilities are very flexible and you can easily create your own Flash Implementations by doing two things:
    1.
    Create a CFC that inherits from coldbox.system.web.flash.AbstractFlashScope
    2.
    Implement the following functions: clearFlash(), saveFlash(), flashExists(), and getFlash()

Implementable Methods

Method
ReturnType
Description
clearFlash()
void
Will destroy or clear the entire flash storage structure.
saveFlash()
void
Will be called before relocations or on demand in order to flash the storage. This method usually talks to the getScope() method to retrieve the temporary flash variables and then serialize and persist.
flashExists()
boolean
Checks if the flash storage is available and has data in it.
getFlash()
struct
This method needs to return a structure of flash data to reinflate and use during a request.
Caution It is the developer's responsibility to provide consistent storage locking and synchronizations.
All of the methods must be implemented and they have their unique purposes as you read in the description. Let's see a real life example, below you can see the flash implementation for the session scope:
1
/**
2
*********************************************************************************
3
* Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
4
* www.coldbox.org | www.luismajano.com | www.ortussolutions.com
5
********************************************************************************
6
* This flash scope is smart enough to not create unecessary session variables
7
* unless data is put in it. Else, it does not abuse session.
8
* @author Luis Majano <[email protected]>
9
*/
10
component extends="coldbox.system.web.flash.AbstractFlashScope" accessors="true"{
11
12
// The flash key
13
property name="flashKey";
14
15
/**
16
* Constructor
17
* @controller.hint ColdBox Controller
18
* @defaults.hint Default flash data packet for the flash RAM object=[scope,properties,inflateToRC,inflateToPRC,autoPurge,autoSave]
19
*/
20
function init( required controller, required struct defaults={} ){
21
super.init( argumentCollection=arguments );
22
23
variables.flashKey = "cbox_flash_scope";
24
25
return this;
26
}
27
28
/**
29
* Save the flash storage in preparing to go to the next request
30
* @return SessionFlash
31
*/
32
function saveFlash(){
33
lock scope="session" type="exclusive" throwontimeout="true" timeout="20"{
34
session[ variables.flashKey ] = getScope();
35
}
36
return this;
37
}
38
39
/**
40
* Checks if the flash storage exists and IT HAS DATA to inflate.
41
*/
42
boolean function flashExists(){
43
// Check if session is defined first
44
if( NOT isDefined( "session" ) ) { return false; }
45
// Check if storage is set and not empty
46
return ( structKeyExists( session, getFlashKey() ) AND NOT structIsEmpty( session[ getFlashKey() ] ) );
47
}
48
49
/**
50
* Get the flash storage structure to inflate it.
51
*/
52
struct function getFlash(){
53
if( flashExists() ){
54
lock scope="session" type="readonly" throwontimeout="true" timeout="20"{
55
return session[ variables.flashKey ];
56
}
57
}
58
59
return {};
60
}
61
62
/**
63
* Remove the entire flash storage
64
* @return SessionFlash
65
*/
66
function removeFlash(){
67
lock scope="session" type="exclusive" throwontimeout="true" timeout="20"{
68
structDelete( session, getFlashKey() );
69
}
70
return this;
71
}
72
73
}
Copied!
As you can see from the implementation, it is very straightforward to create a useful session flash RAM object.
Copy link