【发布时间】:2012-05-26 17:55:21
【问题描述】:
我正在为 cakephp 编写博客/用户身份验证教程,并尝试添加一些功能来掌握事情的窍门。我现在在这个身份验证页面上 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
我试图仅在用户登录时显示注销链接,仅在用户注销时显示登录链接。我正在尝试遵循这篇帖子 CakePHP check if user is logged in inside a view 的建议,但我有点困惑。
edit- 为了确保正确找到我的 authuser,我只是在其中放了一个 echo "test" 语句。链接工作正常,但是,当我删除 echo 语句时,即使我已登录,链接也只会显示登录名。我无法弄清楚为什么链接只有在我的元素中回显某些内容时才能正常工作。
所以,在我的帖子/索引页面上,我有以下内容
<?php
if($this->element('authuser') == TRUE){
?>
<p><?php echo $this->Html->link('Log In', array('controller'=>'users','action' => 'login')); ?></p>
<?
}
else{
?>
<p><?php echo $this->Html->link('Log Out', array('controller'=>'users','action' => 'logout')); ?></p>
<?
}
?>
我的元素 authuser.ctp 包含
<?
$authuser = AuthComponent::user();
if($authuser){
RETURN TRUE;
}
echo "test"; //when this is commented out the link on posts/index only displays login
?>
【问题讨论】: