Run into a strange problem today. It seems that a object looses it's previously assign data values when passed to an anonymous function with the use
keyword.
i have a flash message handler that looks as follows. note i have removed smarty configuration lines to make the sample cleaner.
$smarty = new Smarty();
$error = getErrorMessage();
$smarty->assign('error', $error);
$success = getSuccessMessage();
$smarty->assign('success', $success);
the above assignment is done in the global scope and right after the assignment if i execute the following code.
$error = $smarty->getTemplateVars('error');
echo $error; //displays the proper value
i can see the value is properly set. but when the application reached slim action for the value seems to be lost.
$app->get("/password", function () use ($app, $smarty) {
$error = $smarty->getTemplateVars('error');
echo $error; //displays empty string
$smarty->display('password.tpl');
});
due to this problem the value assigned at the global scope never reach the template. doing {debug} inside the template shows the variable value is empty. However it shows there is a variable named error
and success
with an empty value.
anyone have an idea why this could be?
UPDATE
after adding the following code block to flash message retrieval function. I can see that this function is called for a 2nd time once the original flash message value is retrieved.
function getSessionMessage($key)
{
$msg = "";
syslog(LOG_WARNING, "Access" . microtime() . $key);
if (isset($_SESSION[$key])) {
$msg = $_SESSION[$key];
syslog(LOG_WARNING, "Access" . microtime() . $msg);
unset($_SESSION[$key]);
}
return $msg;
}
but why would it be called twice? my script only runs once and there is no incorrect URLs that would cause to trigger another slim execution cycle.
UPDATE 2
further diagnoses shows that flash-retrieval function is called twice. However apache access logs only show one request coming from the browser.
Jun 15 13:35:14 beast apache2: Access0.68291800 1402814114 NEW successGET
Jun 15 13:35:14 beast apache2: Access0.68419200 1402814114 NEW successGET
127.0.0.1 - - [15/Jun/2014:13:35:14 +0700] "GET /cunm/cunm/password HTTP/1.1" 200 1651 "http://localhost/cunm/cunm/password" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36"
it seems that something is causing the function to be executed twice.
PROBLEM
so i finally found the problem, it appears that if you use grouping in slim framework the code inside the group function gets executed even when the URL is not matching.
$app->group("/admin", function () use ($app, $smarty) {
//any code here runs even with the URL is not match "/admin"
$app->get("/servicearea", function () use ($app, $smarty) {
}
}