使用CakePHP验证时未定义的方法stdClass的::用户()错误 [英] Undefined method stdClass::user() error when using CakePHP Auth

查看:150
本文介绍了使用CakePHP验证时未定义的方法stdClass的::用户()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的CakePHPand是建立使用验证组件的网站。有几次我试图做的事情与此组件已导致错误

I'm fairly new to CakePHPand am building a site using the Auth component. A couple of times I have tried to do things with this component which have caused the error

Fatal error: Call to undefined method stdClass::user() in /ftphome/site/app/controllers/users_controller.php on line 395

在这种情况下,它指的是该生产线是

The line it refers to in this case is

$this->User->read(null, $this->Auth->user('id'));

当我恢复了code回到它怎么是错误之前,我只似乎能够在服务器上删除一些文件,以摆脱它这个错误不会消失(我不知道这文件,当我试图在错误坚持,让 TMP 目录中删除所有文件,我删除了整个网站,并从最新的SVN revesion恢复。

This error does not disappear when I revert the code back to how it was before the error and I only seem to be able to get rid of it by removing some files on the server (I'm not sure which files, when I tried removing all files in the tmp directory the error persisted so I removed the entire site and restored from the latest svn revesion.

在这种特殊情况下,我认为我通过将以下code在 app_controller

In this particular case I think I caused the error by putting the following code in app_controller

class AppController extends Controller {
    function beforeRender() {
        $this->set('test', $this->Auth->user());
    }
}

这是我从这个线程<复制的href=\"http://groups.google.com/group/cake-php/browse_thread/thread/ee9456de93d2eece/cff6fe580d13622b?lnk=gst&q=auth\" rel=\"nofollow\">http://groups.google.com/group/cake-php/browse_thread/thread/ee9456de93d2eece/cff6fe580d13622b?lnk=gst&q=auth

一个$ P $我试图通过更新数据库中的用户后更新验证用户详细信息导致此问题pvious时间。

A previous time I caused the issue by attempting to update the Auth user details after updating the user in the database.

我可以看到我莫名其妙地从验证对象中删除用户对象,但我不明白为什么我需要在网站上删除文件取回或code上面怎么删除它 - 任何帮助将是非常美联社preciated。

I can see I'm somehow removing the user object from the Auth object but I can't understand why I need to delete files in the site to get it back or how the code above removes it - any help would be much appreciated.

修改

它看起来像在情况下,我上面提到的,问题是,我会复制到我的应用程序/控制器 app_controller.php 文件目录。只是有一个空的类声明的文件会导致这个错误 - 任何人都可以提供进一步的深入了解

It looks like in the case I mentioned above, the problem was the app_controller.php file which I'd copied into my app/controllers directory. Just having the file with an empty class declaration causes this error - can anyone provide further insight?

进一步编辑

我已经意识到我已经有点傻了,问题就由我把 app_controller.php /应用/控制器/所致app_controller.php 时,已经有之一 /app/app_controller.php - 感谢输入,虽然安迪,它可以帮助我了解多一点发生了什么事。

I've realised I've been a bit silly and the problem was caused by me putting app_controller.php in /app/controllers/app_controller.php when there was already one in /app/app_controller.php - thanks for the input though Andy, it helps me understand a bit more what was happening.

推荐答案

,然后重新读取/ deserialised在另一请求时,一个类实例(在你的情况下验证的一个实例)已系列化到磁盘此错误通常是抛出但类定义(即验证)还没有被加载呢,所以PHP创建它作为一个stdClass的(标准级)

This error is normally thrown when a class instance (in your case an instance of Auth) has been serialised to disk, then re-read/deserialised in another request but the class definition (i.e. Auth) has not been loaded yet, so PHP creates it as an "stdClass" (standard class.)

当您删除站点文件,您要移除会话存储(IIRC它在一个CakePHP的应用程序缓存文件夹),因此在接下来的请求时,一个新的会话从头开始创建。

When you remove your site files, you're removing the session storage (IIRC it's the Cache folder in a CakePHP app) so at the next request, a new session is created from scratch.

这已经有一段时间,因为我最后一次使用CakePHP的(我切换到Zend的),所以我不记得,如果蛋糕包括文件,它需要使用__autoload函数或没有。

It's been a while since I last used CakePHP (I switched to Zend) so I cannot remember if Cake includes files it requires using an __autoload function or not.

在该邮件列表的帖子,有人说,你可以这样$这个 - > Auth->用户()在视图中,但在控制器,可以使用$会话级>阅读('Auth.User')来得到用户的组成部分。不知道有什么区别,也许这 - $>验证是一个视图助手,所以不可用控制器?

On that mailing list post, someone says that you can this $this->Auth->user() in a view, but in the controller, you can use $session->read('Auth.User') to get the user component. Not sure what the difference is, maybe $this->Auth is a view helper, so isn't available in the controller?

这篇关于使用CakePHP验证时未定义的方法stdClass的::用户()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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