首先,我对Laravel相当新鲜。
所以我在每个页面的标题中都有一个表单,指向POST请求。我知道我的mySQL数据库中有正确的用户电子邮件和密码但是当我尝试登录'if(Auth :: attempt($ userdata))时总是失败。
形式:
@if(Auth::check())
<li><a href="{{ URL::to('/') }}">Home</a></li>
<li><a href="{{ URL::to('profile') }}">Profile</a></li>
<li><a href="{{ URL::to('logout') }}">Logout</a>
@else
<div class="navbar-form navbar-left">
{{ Form::open(array('url' => '/account/sign-in')) }}
<div class="form-group">
<input type="email" name="email" class="form-control input-sm" placeholder="Email">
<input type="password" name="password" class="form-control input-sm" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-default btn-sm">Login</button>
{{ Form::close() }}
POST:
Route::post('/account/sign-in', function(){
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if(Auth::attempt($userdata)){
return 'Success';
}else{
return 'Fail';
}
});
我还在我的User.php模型中包含$ fillable:
protected $fillable = array('firstname', 'lastname', 'password', 'email', 'code', 'active');
注册方法:
public function postCreate(){
$validator = Validator::make(Input::all(),
array(
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|max:50|email', // 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
'password_confirmation' => 'required|same:password'
)
);
if($validator->fails()){
return Redirect::route('account-create')->withErrors($validator)->withInput();
}else{
//Activation Code
$code = str_random(60);
$user = new User;
$pass1 = Input::get('password');
$pass2 = Input::get('password_confirmation');
$firstname = Input::get('firstname');
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->email = Input::get('email');
$user->code = $code;
$user->active = 0;
if($pass1 == $pass2){
$user->password = Hash::make(Input::get('pass1'));
}else{
return Redirect::to('register/');
}
$user->save();
}
if($user){
//send email
Mail::send('emails.auth.activate', array('link' =>URL::route('account-activate', $code), 'firstname' => $firstname), function($message) use($user){
$message->to($user->email, $user->firstname)->subject('Activate your Account');
});
return Redirect::to('/')->with('global', 'Your account has been create! We have sent you an email to activate your account');
}
}
在UsersController中登录方法:
public function postSignIn(){
$validator = Validator::make(Input::all(),
array(
'email' => 'required',
'pasword' => 'required'
)
);
if($validator->fails()){
return Redirect::to('/')->withErrors($validator)->withInput();
}else{
$user = array(
'active' => 1,
'email' => Input::get('email'),
'password' => Input::get('password')
);
if(Auth::attempt($user)){
//Redirect to intended page
return Redirect::intended('/')->with('global', 'Success!!');
}else{
/*return Redirect::to('/')->with('global', 'Failed to login');*/
return Input::all();
}
}
return Redirect::to('/')->with('global', 'There was a problem signing in. Did you activate?');
}
查看:
<ul class="nav navbar-nav navbar-right">
@if(Auth::check())
<li><a href="{{ URL::to('/') }}">Home</a></li>
<li><a href="{{ URL::to('profile') }}">Profile</a></li>
<li><a href="{{ URL::to('logout') }}">Logout</a></li>
@else
<div class="navbar-form navbar-left">
{{ Form::open(array('url' => '/account/sign-in')) }}
<div class="form-group">
<input type="email" name="email" class="form-control input-sm" placeholder="Email">
<input type="password" name="password" class="form-control input-sm" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-default btn-sm">Login</button>
{{ Form::close() }}
</div>
<li><a href="{{ URL::to('account/create') }}">Register</a></li>
@endif
</ul>
由于
答案 0 :(得分:0)
来自您在Dropbox的文件,postSignIn
UsersController
方法'pasword' => 'required'
已{。}}。