2017-02-27 57 views
2

使用我的代碼能夠正常工作,但現在有一個錯誤laravel 4未定義指數:密碼

(ErrorException(E_NOTICE)未定義指數:密碼)

我真的不知道爲什麼,所以如果你知道請幫助我。我搜索谷歌類似的異常,並嘗試我看到的一切,但仍然沒有成功...這裏是我的代碼

route.php

Route::resource('login', 'LoginController'); 

的LoginController

 public function store(){ 

     /*$credentials = array(
      'username' => Input::get('username'), 
      'password' => Hash::make(Input::get('password')), 
     );*/ 

     if(Auth::attempt(['active' => 1])) { 

      // if(Auth::attempt($credentials)){ 

      if(Auth::attempt(Input::only('username', 'password'))){ 

       return Auth::user(); 
       //return Redirect::back()->with('message','You are now logged in!'); 
       //return Redirect::to('/')->with('message','You are now logged in!'); 
      } 
      else{ 
       //$errors = ['password' => "Username and/or password invalid."]; 
       //return Redirect::back()->withErrors($errors)->withInput(Input::except('password')); 
       return 'Failed~'; 
      } 
     } 
     else{ 
      //$errors = ['password' => "Please check your email to activate your account"]; 
      //return Redirect::back()->withErrors($errors)->withInput(Input::except('password')); 
      return 'Failed~'; 
     } 
    } 
} 

查看登錄/ create.blade .PHP

@section('content') 
    <div class="col-lg-3"></div> 
    <div class="form col-lg-6 box"> 

     {{ Form::open(['route' => 'login.store'], array('class' => 'form-horizontal')) }} 

    {{-- @if ($error = $errors->first('password')) 
      <div class="alert alert-danger"> 
       {{ $error }} 
      </div> 
     @endif--}} 
     <div class="input-group form-group-md"> 
      <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> 
      {{ Form::text('username', null,['class' => 'form-control','autofocus'=>'autofocus', 'placeholder' => 'Username']) }} 
     </div> 
     <div class="input-group form-group-md"> 
      <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span> 
      {{ Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password')) }} 
     </div> 

     <div class="form-group-md"> 
      <a href="/home" class="link">Forgot your password?</a> 
     </div> 
     <div class="form-group-md"> 
      {{ Form::submit('Log In', array('class' => 'login navbar-btn btn-block form-control', 'autofocus'=>'autofocus')) }} 
     </div> 

     <div class="question form-group-md"> 
      {{ Form::label('register', 'Do not have an account?! ') }} <a href="{{ url('users/create') }}" class="link">Register here!</a> 
     </div> 
    {{ Form::close() }} 
    </div> 
    <div class="col-lg-3"> </div> 
@stop 

error

+1

這個錯誤在哪一行不是? – Naincy

回答

0

重寫你的代碼如下。

$username = Input::get('username'); 
$password = Input::get('password'); 
// You also may add extra conditions to the authenticating query 
if (Auth::attempt(array('username' => $username, 'password' => $password, 'active' => 1))) 
{ 
    // The user is active, not suspended, and exists. 
}else{ 
    // user not exist or suspended 
} 

也請參考這個link也。

+0

這完美謝謝 –

+0

歡迎。樂於幫助 :-) –

0

的問題是,所述validateCredentials方法,其是屬於你的類提供者EloquentUserProvider預計,$credentials陣列具有數組元素稱爲password,我們期望它被散列。

如圖here

/** 
* Validate a user against the given credentials. 
* 
* @param \Illuminate\Contracts\Auth\Authenticatable $user 
* @param array $credentials 
* @return bool 
*/ 
public function validateCredentials(UserContract $user, array $credentials) 
{ 
    $plain = $credentials['password']; 
    return $this->hasher->check($plain, $user->getAuthPassword()); 
} 

,你在你的代碼發送錯誤的數組的第一次通話

if(Auth::attempt(['active' => 1]))

解決這個你可能要重寫你的代碼的一些事情像這樣,

public function store() 
{ 
    $credentials = array(
     'username' => Input::get('username'), 
     'password' => Hash::make(Input::get('password')), 
     'active' => 1 
    ); 

    if(Auth::attempt($credentials)) { 
     return Auth::user(); 
    } else { 
     return 'Failed~'; 
    } 
}