Laravel 4与Confide Auth :: check()混淆了JSON响应

时间:2014-06-25 09:36:31

标签: php jquery json laravel-4

我有一个非常简单的方法,可以在表格中创建新记录。但我只希望登录用户允许此操作(Laravel / Confide)。一切正常,无需检查用户是否已登录。该脚本很好地将JSON字符串返回给我的jQuery Ajax调用。

但是只要我添加'if(Auth :: check()'检查我在结果中得到一个巨大的额外字符串(完整的User类!?!?!并附加了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"]

1 个答案:

答案 0 :(得分:0)

首先,我确信错误出现在Confide中。但缺乏评论/答案以及我无法在互联网上找到任何关于它的事实我深入研究了代码。结果是5分钟的工作&#39;我修好了我犯了一个很大的错误!

我想保留旧用户模型,以防其他时间需要它。因此,在安装Confide时,我将用户模型更改为:

<?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巨大的错误;)