我正在使用 slimphp 框架,并且对路由函数的变量范围感到困惑。我有以下内容:
$app = new \Slim\Slim(array(
'templates.path' => '../templates/',
'view' => new Slim\Extras\Views\Twig()
));
$user = SES\Session::getInstance();
include 'routes.php';
$app->run();
//Routes.php
$app->get("/", function() use ($app, $user){
....
}
Routes.php
如果我不添加global $user;
到顶部,则失败:
Notice: Undefined variable: user
为什么我可以$app
从此文件访问 var 但无法访问$user
var?