如何定义一个全局变量,以便我的current_user方法可以工作,我想要它,我只需要检查是否有当前用户,我的示例代码如下
if (isset($_SESSION['company_id'])) {
$current_user = Companies::find($_SESSION['company_id']);
}
else
{
$current_company = null;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使用当前的用户方法,无需将其传递给我的app->render()方法,就像在我的header.html中一样
{% if current_user %}
hi {{current_user.name}}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
注入在回调函数中不起作用。
要访问回调函数中的变量,您可以使用"use() "function :
$mydata = array ( ... );
$app->get('/api', function(Request $request, Response $response) use($mydata) {
echo json_encode($mydata);
});
Run Code Online (Sandbox Code Playgroud)
我终于能够让它与这个一起工作
$app->hook('slim.before.dispatch', function() use ($app) {
$current_company = null;
if (isset($_SESSION['company_id'])) {
$current_company = Company::find($_SESSION['company_id']);
}
$app->view()->setData('current_company', $current_company);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13006 次 |
| 最近记录: |