【问题标题】:laravel 4 Undefined index: passwordlaravel 4未定义索引:密码
【发布时间】:2017-02-27 09:22:49
【问题描述】:

我的代码以前可以正常工作,但现在出现错误

(ErrorException (E_NOTICE)未定义索引:密码)

我真的不知道为什么,所以如果你知道请帮助我。我在谷歌上搜索了类似的异常并尝试了我看到的所有内容,但仍然没有成功......这是我的代码

路由.php

Route::resource('login', '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

【问题讨论】:

  • 这个错误在哪一行没有?

标签: php laravel laravel-4


【解决方案1】:

如下重写你的代码。

$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

【讨论】:

  • 欢迎。乐于助人:-)
【解决方案2】:

问题是属于您的类提供者EloquentUserProvidervalidateCredentials 方法期望$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' =&gt; 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~';
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2018-08-02
    • 2012-08-24
    • 2015-03-23
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    相关资源
    最近更新 更多