CakePHP未定义索引错误 [英] CakePHP undefined index error

查看:108
本文介绍了CakePHP未定义索引错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试访问已登录的用户的电子邮件地址,而且我可以在所有控制器中成功登录,除了一个。

I'm trying to access the email address for a user that's signed in and I can do so successfully in all of my Controllers except for one.

这里是注意(8):未定义的索引:用户[APP / View / Layouts / default.ctp,我将获得

Here is the error I'm getting

Notice (8): Undefined index: User [APP/View/Layouts/default.ctp, line 37]

这里是相应的代码行(记住,控制器)。

And here is the corresponding line of code (remember, this works in all of my other controllers).

<center><font color="black"><td><?php echo $user['User']['email']; ?></td></text></center>

以下是DemosController

Here is the DemosController

<?php
// app/Controller/DemosController.php
class DemosController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add','logout');

        $user = $this->Demo->read(null, $this->Auth->user('id'));  //this throws the error
        //$user = $this->Demo->User->read(null, $this->Auth->user('id'));  //Fatal error: Call to a member function read() on a non-object in
        $this->set('user', $user);

    }

    //display the knockout table
    public function index($article = null) {

    }


}



我不需要在此控制器的数据库中使用表。它只是为了演示的目的显示一个表。

I do not need a table in the database for this Controller. It's simply to display a table for demo purposes. Can I just let it reference user?

class Demo extends AppModel {
    public $name = 'User';         
}

为什么我可以在我的所有控制器是模型/表情况导致错误?如果是,有没有办法禁用表的使用?

Why can I access this in all of my controllers except this one? Is the Model/table situation causing the error? If so, is there a way to disable the use of a table?

推荐答案

通过AuthComponent获取登录用户的属性



不要必须通过'viewVar'将当前登录的用户的信息传递到视图。
要访问控制器之外当前登录的用户的属性,请使用 AuthComponent

Getting properties of the logged-in user via AuthComponent

You don't have to pass information of the currently logged-in user to the view via a 'viewVar'. To access properties of the currently logged-in user outside of the controller, use the 'static' interface of the AuthComponent

在您的视图或布局内,输出如下信息:

Inside your views or layouts, output the information like this;

<p>The current users email is: <?php echo AuthComponent::user('email') ?></p>
<p>The current users id is: <?php echo AuthComponent::user('id') ?></p>
<p>And all properties of the user are:</p>
<pre><?php print_r(AuthComponent::user());?></pre>

请参阅: 访问登录用户

您可以删除这些行从 beforeFilter();

$user = $this->Demo->read(null, $this->Auth->user('id'));  //this throws the error
//$user = $this->Demo->User->read(null, $this->Auth->user('id'));  //Fatal error: Call to a member function read() on a non-object in
$this->set('user', $user);

这篇关于CakePHP未定义索引错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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