2014-06-25 116 views
0

我有一個非常簡單的方法在表中創建一個新記錄。但我只想要登錄用戶允許的這種操作(Laravel/Confide)。所有工作正常,無需檢查用戶是否已登錄。該腳本很好地將JSON字符串返回給我的jQuery Ajax調用。Laravel 4與Confide Auth :: check()混淆了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"] 

當我添加認證檢查我得到這個結果:

<!-- 
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"] 
+0

您介意發佈'json'視圖文件的內容嗎?如果你只是想發送一個json響應,你不能只是'返回Response :: json('json'=> ['success'])'? https://gist.github.com/anonymous/01b39d781945fff6bdba – thpl

+0

<?php header('Content-type:application/json'); ?> {{json_encode($ json)}} –

+1

您不應該在視圖中設置內容類型。我不知道laravel在內部對視圖做出反應時的全部過程,但是有些後臺處理以您的方式發生的可能性很高。你最好使用'Response :: json()'。看看我在第一條評論中提供的代碼是否適合你。 – thpl

回答

0

首先我確信錯誤是在Confide中。但缺乏評論/答案,以及我無法在互聯網上找到任何有關它的事實,我都深入瞭解代碼。結果是5分鐘的'工作',我修好了。我犯了一個很大的錯誤!

我想保留舊的用戶模型,以防其他時間需要它。因此,在安裝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巨大的錯誤;)