我有一个非常简单的方法,在表中创建一个新记录。但我只希望登录用户(Laravel/Confide)允许此操作。所有工作正常,不检查用户是否登录。该脚本很好地向jQuery Ajax调用返回一个JSON字符串。
但只要我添加'if(Auth::check()'检查我得到一个巨大的额外字符串的结果(完整的用户类!?!?!与JSON编码的数组附加到它),搞乱了我的JSON编码数组。这是正常的行为还是我遗漏了什么?
这是工作方法:
public function create()
{
//if(Auth::check()) {
//$userId = Auth::getUser()->id; // Get logged in user id
$folder = new Folder();
$folder->parent_id = Input::get('fid');
$folder->title = 'Nieuwe map';
$folder->alias = 'nieuwe-map-'.time();
//$folder->created_by = $userId;
$folder->save();
return Response::json(array('success'));
//} else {
//return Response::json(array('error'));
//}
}
结果如下:
["success"]
当我添加Auth检查时,我得到这个结果:
<!--
use Illuminate'Auth'UserInterface;
use Illuminate'Auth'Reminders'RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->remember_token;
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->remember_token = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'remember_token';
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
} -->["success"]
首先我确信错误是在保密。但由于缺乏评论/答案,而且我在互联网上找不到任何关于它的信息,所以我开始深入研究代码。结果是5分钟的"工作",我修复了它。我犯了一个大错误!
我想保留旧的User模型,以备将来需要时使用。因此,在安装Confide时,我将User模型更改为:
<?php
use Zizaco'Confide'ConfideUser;
class User extends ConfideUser {
}
?>
<!--
use Illuminate'Auth'UserInterface;
use Illuminate'Auth'Reminders'RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->remember_token;
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->remember_token = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'remember_token';
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
} -->
所以…就是这个。当调用Auth:check()时,未注释的代码被打印出来:S巨大的错误;)