CakePHP检查用户是否在视图中登录 [英] CakePHP check if user is logged in inside a view

查看:75
本文介绍了CakePHP检查用户是否在视图中登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 <?php 

if - > Auth-> user())
{
echo $ this-> element('header');
}
else
{
echo $ this-> element('header-bar');
}

?>

在我的视图内,应该显示一个不同的标题为登录用户,但抛出以下错误:注意(8):未定义属性:View :: $ Auth [APP / views / layouts / page.ctp,第17行] $(


$ b <$ c
致命错误:调用成员函数user()on /Users/cameron/Sites/thehive/app/views/layouts/page.ctp on line 17上的非对象

如何解决这个问题?感谢

解决方案

请注意:另请参阅meotimdihia的下面回答






Auth组件用于控制器。你需要在控制器中检查授权,然后为视图设置一个变量,例如 $ this-> set('authUser',$ this-> Auth-> user )); 。然后在您的视图中,您可以:

  if(!$ authUser)
{
echo $ this - > element('header');
}

如果您希望所有控制器方法都自动完成,修改 cake / libs / controller / app_controller.php ,使其包含Auth组件。


I have the following code:

    <?php

    if (!$this->Auth->user())
    {
        echo $this->element('header');
    }
    else
    {
        echo $this->element('header-bar');
    }

    ?>

inside my view which should show a different header for logged in users but throws the following error:

Notice (8): Undefined property: View::$Auth [APP/views/layouts/page.ctp, line 17]
Fatal error: Call to a member function user() on a non-object in /Users/cameron/Sites/thehive/app/views/layouts/page.ctp on line 17

How do I fix this? Thanks

解决方案

Note: Also check out meotimdihia's answer below. It's got a lot of upvotes.


The Auth component is for use in the Controller. You'll want to check for authorization in the controller, then set a variable for the view, e.g., $this->set('authUser', $this->Auth->user());. Then in your view you can do:

if (!$authUser)
{
    echo $this->element('header');
}

If you want this to be done automatically for all controller methods, you can look into modifying cake/libs/controller/app_controller.php so that it includes the Auth component.

这篇关于CakePHP检查用户是否在视图中登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆