【发布时间】:2012-12-18 17:32:20
【问题描述】:
我正在使用 Laravel,我刚刚将在本地运行的代码移动到 Live 中,并且在我的“用户”控制器中出现以下异常:
Unhandled Exception
Message:
Using $this when not in object context
奇怪的是,这是一个类并且在本地工作得很好,所以我不希望解决方案使用静态符号。只有当我将其推广到 Live 时,我才会收到此错误。会不会是 Laravel 核心中的某些东西在 Live 中没有正确加载控制器类?
有没有人在将他们的代码推广到 Live 后遇到过这种情况?有什么想法吗?
更新:发生错误的代码的 sn-p。请记住此代码在本地工作,因此我认为缺少某些内容,而不是需要更改此代码以解决此问题。
class User_Controller extends Base_Controller {
...
public function action_register() {
...
if ($user) {
//Create the Contact
DB::transaction(function() use ($user_id) {
$org = $this->create_org($user_id); //failing on this line with exception. btw $user is created fine
$this->create_contact($org->id);
$this->create_address($org->id);
});
private function create_org($user_id) {
$result = Org_type::where('name','=',$_POST['org_type'])->first();
$org = Org::Create(
array(
'name' => $_POST['org_name'],
'user_id' => $user_id,
'org_type_id' => $result->id,
)
);
return $org;
}
...
【问题讨论】:
-
你能发布堆栈跟踪和/或完整的函数/类代码吗?希望它可以帮助我们查明问题:)
-
我也刚刚注意到 - 这是在静态函数中吗?
$this在静态函数中不起作用。 -
感谢 Daniel,刚刚更新了代码 sn-p。没有对 $this 的引用带有一个非静态的控制器功能。它在我的笔记本电脑上本地运行良好:-0
-
添加了我的答案,试一试,希望可以解决您的问题:)
标签: php object controller laravel unhandled-exception