我是Laravel的新手 . 我正在尝试编写一个登录示例,但它显示“302 Found”错误 .
web.php ( Route file ) :
Route::post('login', array(
'uses' => 'UserController@doLogin'
));
Route::get('logout', array('uses' => 'UserController@doLogout'));
Route::group(['middleware' => 'AuthMiddleWare'], function () {
Route::get('/form', function () {
return view('form');
});
Route::get('/createUser', function () {
return view('users');
});
Route::get('logout', array(
'uses' => 'UserController@doLogout'
));
});
"doLogin" function codes which located in UserController:
public function doLogin()
{
$rules = array(
'name' => '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
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('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 {
// create our user data for the authentication
$userdata = array(
'name' => Input::get('name'),
'password' => Input::get('password')
);
$auth = DB::table('users')->where('name', '=', Input::get('name'))->where('password', '=', Input::get('password'))->get()->first();
// 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::to('login');
}
}
}
login form:
<html>
<head>
<title>LOGIN</title>
</head>
<body>
<form action="{{ route('login') }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" name="name">
<input type="text" name="surname">
<input type="submit" value="LOGIN">
</form>
</body>
</html>
当我查看来自网络状态代码的登录请求时出现"302 Found" . 当我尝试使用 $auth 而不是 "Auth::attempt($userdata)" 时,它仍显示相同的错误 . 可能与其他原因有关 . 我怎么能找出什么是错的?提前致谢 .
1 回答
您收到此错误是因为您正在使用
route()
辅助函数,该函数按名称选择路由,并且您没有与/login
路由关联的任何名称 .你可以通过两种方式实现这一目标 .
要么你必须改变
OR
希望这可以帮助