doushi7314 2014-06-15 05:56
浏览 51

使用use关键字传递给匿名函数时,对象丢失数据

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) {
     }

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度