Component Writer's Guide :: Session Persistance
RPCL provides object persistance using the webserver session, this way, you can forget about sending parameter lists through URLs or implement a session mechanism for yourself.
Published properties are automatically serialized/userialized by the framework at the right moment, that is, when you run a Page, the last thing done before shutdown the script, is to serialize all published properties with the latest values and the next time the script is executed, properties are recovered from the session, so you work with your application in the same state it was left in the previous POST.
Simple persistance test
An example, can be tested this way:
- Place a Button component on a Page
- Place a Edit component on a Page
- Place another Button on a Page
- On the OnClick event of Button1, change the color of Edit1 to #FF0000
- On the OnClick event of Button2, change the caption of Edit1 to "Hello, Session!"
If you run this app and click Button1, the color of the edit control will change, and now, click Button2, the text of Edit1 will change, but the Color will remain red, without session persistance, the color will have returned to the initial value.
Storing/retrieving extra info
If you develop a component and want to store/retrieve extra data from the session, you must inherit, at least, from Persistent, and you can override serialize() and unserialize() to store/retrieve extra information using $_SESSION object. That information should be qualified to don't confuse with other information, here is an example on the Chart component (excerpt):
function serialize() { parent::serialize(); // serialize the chart manually; there is no support for serialization // by the libchart; so it's done externally $owner = $this->readOwner(); if ($owner != null) { $prefix = $owner->readNamePath().".".$this->_name.".Chart."; $_SESSION[$prefix."Title"] = $this->_chart->title; $_SESSION[$prefix."LogoFileName"] = $this->_chart->logoFileName; $_SESSION[$prefix."Margin"] = $this->_chart->margin; $_SESSION[$prefix."LowerBound"] = $this->_chart->lowerBound; $_SESSION[$prefix."LabelMarginLeft"] = $this->_chart->labelMarginLeft; $_SESSION[$prefix."LabelMarginRight"] = $this->_chart->labelMarginRight; $_SESSION[$prefix."LabelMarginTop"] = $this->_chart->labelMarginTop; $_SESSION[$prefix."LabelMarginBottom"] = $this->_chart->labelMarginBottom; } } function unserialize() { parent::unserialize(); // first manually unserialize the chart $owner = $this->readOwner(); if ($this->_chart != null && $owner != null) { $prefix = $owner->readNamePath().".".$this->_name.".Chart."; if (isset($_SESSION[$prefix."Title"])) $this->_chart->title = $_SESSION[$prefix."Title"]; if (isset($_SESSION[$prefix."LogoFileName"])) $this->_chart->logoFileName = $_SESSION[$prefix."LogoFileName"]; if (isset($_SESSION[$prefix."Margin"])) $this->_chart->margin = $_SESSION[$prefix."Margin"]; if (isset($_SESSION[$prefix."LowerBound"])) $this->_chart->lowerBound = $_SESSION[$prefix."LowerBound"]; if (isset($_SESSION[$prefix."LabelMarginLeft"])) $this->_chart->labelMarginLeft = $_SESSION[$prefix."LabelMarginLeft"]; if (isset($_SESSION[$prefix."LabelMarginRight"])) $this->_chart->labelMarginRight = $_SESSION[$prefix."LabelMarginRight"]; if (isset($_SESSION[$prefix."LabelMarginTop"])) $this->_chart->labelMarginTop = $_SESSION[$prefix."LabelMarginTop"]; if (isset($_SESSION[$prefix."LabelMarginBottom"])) $this->_chart->labelMarginBottom = $_SESSION[$prefix."LabelMarginBottom"]; } }
Restoring the session
It may be useful, specially when developing your application, to get the session to an initial state, because when you make changes in your code, the session state can lead to unexpected results in your app, to destroy the session without closing the browser, you can run your scripts by adding this parameter:
yourscript.php?restore_session=1
The IDE of RadPHP XE, when you run, it always set this parameter to true to ensure the right results.