Laravel还记得我,用Auth :: try运行吗?

时间:2014-08-24 12:22:43

标签: php laravel

所以我有这段代码:

public function postLogin() {
    // validate the info, create rules for the inputs
    $rules = array(
        'username'    => 'required', // make sure the email is an actual email
        'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
    );

    // run the validation rules on the inputs from the form
    $validator = Validator::make(Input::all(), $rules);

    // if the validator fails, redirect back to the form
    if ($validator->fails()) {
        return Redirect::route('login')
            ->withErrors($validator) // send back all errors to the login form
            ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
    } else {

        $remember = (Input::has('remember')) ? true : false;
        // create our user data for the authentication
        $userdata = (array(
            'username'  => Input::get('username'),
            'password'  => Input::get('password')
        ), $remember);

        // attempt to do the login
        if (Auth::attempt($userdata)) {

            // validation successful!
            // redirect them to the secure section or whatever
            // return Redirect::to('secure');
            // for now we'll just echo success (even though echoing in a controller is bad)
            echo 'SUCCESS!';

        } else {

            // validation not successful, send back to form
            return Redirect::route('login')
             ->with('global', 'Incorrect username or password. Please try again.');
        }
    }
}

当它运行时,我得到:syntax error, unexpected ','。基本上它并不期望$ remember会被传递到那里。这意味着什么?我试过把它放在这里:Auth::attempt($userdate), $remember) { }但是那也没有用。它有同样的错误。不知道发生了什么。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以在Authcontroller中使用Auth::viaRemember()来检查用户是否已被记录:

if (Auth::check() || Auth::viaRemember()) {...

并按如下方式更改您的登录检查:

//Assuming, the remember-input is a checkbox and its value is 'on'
if (Auth::attempt($userData, (Input::get('remember') == 'on') ? true : false)) {...