2014-05-02 84 views
5

我有兩個表在我的數據庫 1:項目 3:用戶 並試圖登錄用戶,它工作正常,但我的問題是,我沒有提到從哪個表中它將獲取數據,但它是從所需的表中自動獲取?這是Laravel的一個特性還是我做錯了什麼? 或者我不明白爲什麼會發生這種情況?Laravel如何驗證::嘗試知道表

形式

{{ Form::open(array('class'=> 'forming')) }} 

    {{ Form::text('username',null,array('placeholder' => 'Username' ,'class' => 'insi')); }}<br> 
    {{ Form::password('password',array('placeholder'=>'Password', 'class'=>'paswor')); }}<br> 
    {{ Form::submit('SignIn!',array('class'=> 'but-n')); }} 

{{ Form::close() }} 

和AuthController

class AuthController extends Controller{ 

     public function getLogin(){ 
      return View::make('login'); 
     } 

     public function postLogin(){ 
      $rules = array('username'=> 'required', 'password'=>'required'); 

      $validator = Validator::make(Input::all(),$rules); 

      if($validator->fails()){ 
       return Redirect::route('login') 
        ->withErrors($validator); 
      } 

      $auth = Auth::attempt(array(
       'username' => Input::get('username'), 
       'password' => Input::get('password') 
       ), false); 
      if(!$auth){ 

       return Redirect::route('login') 
        ->withErrors(array(
         'Invalid' 
         )); 
      } 

      return Redirect::route('home'); 

     } 
} 

回答

8

驗證使用您在有模型的file app/config/auth.php

如果您使用的是雄辯司機用於驗證:

/* 
|-------------------------------------------------------------------------- 
| Authentication Model 
|-------------------------------------------------------------------------- 
| 
| When using the "Eloquent" authentication driver, we need to know which 
| Eloquent model should be used to retrieve your users. Of course, it 
| is often just the "User" model but you may use whatever you like. 
| 
*/ 

'model' => 'User', 

如果您使用的是數據庫驅動程序

/* 
|-------------------------------------------------------------------------- 
| Authentication Table 
|-------------------------------------------------------------------------- 
| 
| When using the "Database" authentication driver, we need to know which 
| table should be used to retrieve your users. We have chosen a basic 
| default value but you may easily change it to any table you like. 
| 
*/ 

'table' => 'users', 

如果你需要使用一個以上的表做認證,你有一些選擇,其中之一是使用建築像這樣的繼續登錄:

class Logon { 

    public function loginViaEmail($email, $password) 
    { 
     if ($emailModel = Email::where('email', $email)->first()) 
     { 
      return $this->login($emailModel->user_id, $password); 
     } 

     return false; 
    } 

    public function loginViaName($name, $password) 
    { 
     if ($memberModel = Member::where('name', $name)->first()) 
     { 
      return $this->login($memberModel->user_id, $password); 
     } 

     return false; 
    } 

    public function loginViaId($id, $password) 
    { 
     if ($beingModel = Being::where('id', $id)->first()) 
     { 
      return $this->login($beingModel->user_id, $password); 
     } 

     return false; 
    } 

    public function login($id, $password) 
    { 
     $user = Member::find($id); 

     if(Hash::check($password, $user->password)) 
     { 
      Auth::loginUsingId($id); 

      return true; 
     } 

     return false; 
    } 
} 

而在你的控制器現在你可以這樣做:

$logon = new Logon; 

if (! $logon->loginViaEmail(Input::get('email'), Input::get('password'))) 
{ 
    return "username or password invalid"; 
} 

return "logged in successfully"; 

... 

if (! $logon->loginViaName(Input::get('name'), Input::get('password'))) 

... 
+0

如果您使用數據庫驅動程序而不是使用數據庫驅動程序,那麼只需在下面使用該表。 – ceejayoz

+0

好吧然後是雄辯和驗證都具有相同的功能? –

+0

Auth可能會使用Eloquent訪問您的用戶表並對其進行身份驗證,但它們沒有相同的功能,這些完全不同。 –