[英]Phalcon php undefined variable session
I am studying Phalcon and I'm trying to set username to session OOP Phalcon style. 我正在研究Phalcon,并且试图将用户名设置为会话OOP Phalcon样式。 Here is what I have in my controller:
这是我的控制器中的内容:
$users = Users::find(array($conditions, 'bind' => $parameters));
if (count($users) > 0) {
//login
$this->session->start();
$this->session->set("username", $username);
$this->view->successMessage = "You are logged in";
}
This is a chunk of loginAction. 这是loginAction的一部分。 If I print_r($_SESSION);
如果我print_r($ _ SESSION); in login.volt (where user gets redirected after login action) it prints out the session, but in other views when I try to print session I get an error: undefined variable session.
在login.volt中(登录操作后用户将被重定向),它打印出会话,但是在其他视图中,当我尝试打印会话时,出现错误:未定义的变量会话。 In my services.php I have
在我的services.php中
$di->setShared('session', function() {
$session = new Phalcon\Session\Adapter\Files();
$session->start();
return $session;
});
Would be grateful for any help 感谢您的帮助
EDIT Let me put it this way. 编辑让我这样说。 In normal php I could do something like this: username;
在普通的php中,我可以这样做: ?>
?>
And then in view I could do something like: 然后鉴于我可以做类似的事情:
<?php
if (isset($_SESSION['username']))
{
echo '<input type="submit" name="Button" value="button"';
}
?>
What is the equivalent in Phalcon? Phalcon中的等效项是什么?
When you retrieve your shared service via DI::getShared('session')
(in your case in controller via $this->session
) it will start the session for you, so no need to manually start it again. 当您通过
DI::getShared('session')
检索共享服务时(在您的情况下,通过$this->session
在控制器中),它将为您启动该会话,因此无需手动再次启动它。 Here in documentation is pretty much how you should do it. 这里的文件是相当多的,你应该怎么做。
Another point, it would make the most sense to use session bag or persistent storage , second use the same implementation as session bags but kind of a shortcut. 另一点,使用会话包或持久性存储是最有意义的,其次使用与会话包相同的实现方式,但是有点捷径。
And actually, not less of a valid point about retrieving session data in your views – you should pass it as a view variable, not implement that logic in the view (if you want more of a proper Phalcon way of doing stuff). 实际上,在视图中检索会话数据的重要性不亚于–您应该将其作为视图变量传递,而不是在视图中实现该逻辑(如果您想要更多的Phalcon正确的工作方式)。 I missed the part about
print_r($_SESSION)
. 我错过了有关
print_r($_SESSION)
。 Can you actually $this->view->setVar('myValue', 'my session value');
你可以实际
$this->view->setVar('myValue', 'my session value');
and see if that works in other views / controllers. 并查看是否可以在其他视图/控制器中使用。
To set a value to your session bag in one place you do: 要在一个位置为会话包设置值,请执行以下操作:
$users = Users::find(array($conditions, 'bind' => $parameters));
if (count($users) > 0) {
$user = new Phalcon\Session\Bag('user');
$user->username = $username;
}
}
And then to retrieve it else where in another controller and pass it as a variable to view: 然后在另一个控制器中的其他位置检索它,并将其作为变量传递给视图:
$user = new Phalcon\Session\Bag('user');
$this->view->setVar('userName', $user->username);
And then in the volt view to print it: 然后在伏特视图中进行打印:
{% if username %}
<input type="submit" name="Button" value="{{ username }}">
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.