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

Passing global variables to dispatch router library php

所以我使用 noodlehaus / dispatch 进行网站路由。我想将一些变量从主作用域传递给 $currentLangroute(...),但我收到此错误:

Notice: Undefined variable: currentLang in C:\xampp\htdocs\_PERSONAL\newSite\index.php on line 18

这是我的部分代码。

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 ])
        );
    });