我正在为cakephp开发blog / user auth教程并试图添加一些功能来解决问题。我现在正在auth的这个页面上 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的建议,但我有点困惑。
编辑 - 为了确保我的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
?>
答案 0 :(得分:2)
通过调用静态方法
,您始终可以在视图中获取当前用户$authUser = AuthComponent::user();
通过这种方式,您可以在任何地方获得当前的用户,而不是将其从行动中传递出来。
无论如何,我建议您将此逻辑添加到element并重复使用。