嗨我试图制作一个好的用户身份验证表格,但我遇到了一些问题,所以首先我尝试了这个:
Route::get('login', function()
{
/* Get the login form data using the 'Input' class */
$userdata = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
/* Try to authenticate the credentials */
if(Auth::attempt($userdata))
{
// we are now logged in, go to admin
return Redirect::to('home');
}
else
{
return Redirect::to('login');
}
});
这是表单刀片页面:
{{ Form::open(array('url' => 'login', 'class' => 'form-horizontal')) }}
<div class="control-group">
<label class="control-label" for="username"></label>
<div class="controls">
<input id="username" name="username" type="text" placeholder="" class="input-xlarge" required="">
</div>
</div>
<!-- Password input-->
<div class="control-group">
<label class="control-label" for="password"></label>
<div class="controls">
<input id="password" name="password" type="password" placeholder="" class="input-xlarge" required="">
</div>
</div>
<!-- Button -->
<div class="control-group">
<label class="control-label" for="submit"></label>
<div class="controls">
<button id="submit" name="submit" class="btn btn-inverse"></button>
</div>
</div>
</fieldset>
它给了我重定向循环错误
然后我试了一下:
Route::get('login', function()
{
/* Get the login form data using the 'Input' class */
$user = new User;
$log = array(
$user -> username = Input::get('username'),
$user -> password = Input::get('password')
);
/* Try to authenticate the credentials */
if(Auth::attempt($log))
{
// we are now logged in, go to admin
return Redirect::to('home');
}
else
{
return Redirect::to('login');
}
});
它给了我:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `users` where `0` is null and `1` is null limit 1)
有谁知道这个问题是什么? 第一个代码应该有效,但为什么重定向错误?
答案 0 :(得分:1)
首先,将您的路线名称更改为:
Route::get('login', function(){});
和Route::get('sign-in', function(){});
获取身份验证页面
Route::get('login', function()
{
return View::make('your-auth-view');
}
您的sign-in
处理程序应如下所示:
Route::get('sign-in', function(){
$userdata = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::attempt($userdata))
{
// we are now logged in, go to admin
return Redirect::to('home');
}
else
{
return Redirect::to('login');
}
});
表单将更改为:
{{ Form::open(array('url' => 'sign-in', 'class' => 'form-horizontal')) }}
P.S。你有一个重定向循环,因为,你有两个相同的路由,当你提交表单时,你一次又一次地重定向到登录页面
答案 1 :(得分:0)
如果您使用的是Laravel 4.2,并且在添加
后仍然存在问题 {{ Form::token() }}
或
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
然后,您可以尝试通过添加
来更改app目录中的filter.php
文件
您的过滤器上的 Request::getMethod() !== 'GET'
。
Route::filter('csrf', function()
{
if (Request::getMethod() !== 'GET' && Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
这对我也很有用。