将全局变量传递给调度路由器库php



所以我使用noodlehaus/dispatch进行网站路由。我想将一些变量从主作用域(如$currentLang(传递到route(...),但我得到了以下错误:

注意:未定义的变量:currentLangC: 第18行上的\examplep\htdocs\_PERSONAL\newSite\index.php

这是我的一部分代码。

require './functions/dispatch.php';
$currentLang = 'en';
route('GET', '/resume', function () {
$data['lang'] = $currentLang;
return response(
phtml(__DIR__.'/views/resume', ['data' => $data ])
);
});
dispatch();

请帮我做这个。谢谢

添加global $data;解决了问题:

require './functions/dispatch.php';
$currentLang = 'en';
route('GET', '/resume', function () {
global $currentLang;
$data['resume'] = json_decode(
file_get_contents("assets/json/resume-".$currentLang.".json"), true
);
return response(
phtml(__DIR__.'/views/resume', ['data' => $data ])
);
});

或者像这样:

require './functions/dispatch.php';
$currentLang = 'en';
route('GET', '/resume', function () use ($currentLang){
$data['resume'] = json_decode(
file_get_contents("assets/json/resume-".$currentLang.".json"), true
);
return response(
phtml(__DIR__.'/views/resume', ['data' => $data ])
);
});

最新更新