laravel 自动登陆的时间改如何实现?
控制器
public function login()
{
$email =Input::get('email');
$password = Input::get('password');
$remember = Input::get('remember');
// lets validate the users input
$validator = Validator::make(
array(
'email' =>$email,
'password' =>$password
),
array(
'email' => 'required|email',
'password' => 'required'
)
);
if ($validator->fails()){
return Redirect::back()->withErrors($validator)->withInput();
}else{
if( Auth::attempt(array('email' => $email, 'password' => $password),$remember) ){
return Redirect::to('hud');
}else{
$validator->getMessageBag()->add('input', 'Incorrect email or password');
return Redirect::back()->withErrors($validator)->withInput();;
}
}
}
view层
@extends('templates.outs.auth')
@section('content')
<div class="special-form">
<a href="{{ route('home') }}"><img src="{{ \App\Helpers\Helpers::logoUrl() }}" alt=""></a>
<h3 class="text-center">LOGIN</h3>
@if ($errors->first())
<span class="status-msg error-msg">{{ $errors->first() }}</span>
@endif
<hr>
{!! Form::open(array('action' => 'UsersController@login')) !!}
<div class="form-group">
<label for="email" class="color-primary">Email:</label>
{!! Form::text( 'email', null, array('class' => 'form-control', "placeholder" => "Email","autofocus" => "true" )) !!}
</div>
<div class="form-group">
<label for="password" class="color-primary">Password:</label>
{!! Form::password( 'password', array('class' => 'form-control', "placeholder" => "Password" )) !!}
</div>
<div class="form-group">
<label for="remember" class="color-primary">Remember:</label>
<input type="checkbox" name="remember" value="remember me">
</div>
<div class="form-group">
{!! Form::submit( 'Login', array('class' => 'btn btn-primary btn-wide')) !!}
</div>
{!! Form::close() !!}
<p>Don't have an account? <a href="{{ route('register') }}">register</a></p>
</div>
@stop
路由
Route::get('login', function(){ return View::make('login')->with('pTitle', "Login"); })->name('login');
底层实现
public function login(UserContract $user, $remember = false)
{
$this->updateSession($user->getAuthIdentifier());
// If the user should be permanently "remembered" by the application we will
// queue a permanent cookie that contains the encrypted copy of the user
// identifier. We will then decrypt this later to retrieve the users.
if ($remember) {
$this->createRememberTokenIfDoesntExist($user);
$this->queueRecallerCookie($user);
}
// If we have an event dispatcher instance set we will fire an event so that
// any listeners will hook into the authentication events and run actions
// based on the login and logout events fired from the guard instances.
$this->fireLoginEvent($user, $remember);
$this->setUser($user);
}
比如我现在要设置30天都是可以自动登陆的,
前端设置一个name="remember"的checkbox
然后后台在attempt添加一个$remember对象、你只需接收checkbox的值是否为true就行了,因为默认$remember=false
'lifetime' => 43200,
'expire_on_close' => false,