0

我在 Fat Free Framework 中访问全局变量的问题。特别是,唯一的问题是从代码中获取“file_path”变量:

$f3->route('GET /d/@id',
function ($f3) {

    // Lots of DB code, where which in the end returns $file_id and $file_name

    $file = "upload/";
    $file .= $file_id . "/";

    $file .= $file_name;

    $f3->set('file_path', $file);

    $f3->set('content','download.htm');
    echo View::instance()->render('layout.htm')

}
);

然后我从 download.htm 调用 /getfile:

<a href="/getfile"> GET IT! </a>

然后我尝试从另一条路由访问“file_path”变量,但没有 $f3->get('file_path') 返回 NULL:

$f3->route('GET /getfile',
function ($f3) {

    $file = $f3->get('file_path');

    var_dump($file);
}
);

此外,通过 $f3->get() 访问的其他全局变量工作正常。例如

$f3->route('GET /getfile',
    function ($f3) {

    $db = $f3->get('DB');

    var_dump($db);
}
);

完美地得到 $db 变量。更改局部和全局变量名称没有帮助。关于正在发生的事情有什么想法吗?:S

4

1 回答 1

1

file_path只有在您打开时才能访问/d/@id。您必须缓存变量或将其保存在 SESSION 中。您可以访问DB,因为您将其设置在任何路线之外。

$f3->route('GET /d/@id',
function ($f3) {
    // Lots of DB code, where which in the end returns $file_id and $file_name

    $file = "upload/";
    $file .= $file_id . "/";

    $file .= $file_name;

    $f3->set('SESSION.file_path', $file);

    $f3->set('content','download.htm');
    echo View::instance()->render('layout.htm')
}
);


$f3->route('GET /getfile',
function ($f3) {

    $file = $f3->get('SESSION.file_path'); // you might clear the file_path then

    var_dump($file);
}
);
于 2014-07-06T09:45:10.410 回答