我使用routes.php进行身份验证,这不是正确的方法。现在,我正在UserController
中进行身份验证。我正面临着一个问题。
在routes.php
中,我是这样做的。
Route::post('/login',function(){
$cred = Input::only('username','password');
if(Auth::attempt($cred)){
return Redirect::intended('/');
}else{
$error = "Username or password is incorrect.";
return Redirect::to('login', compact('error'));
}
});
Route::get('/', ['middleware' => 'auth', function(){
return view('index');
}]);
现在我使用的是控制器UserController
。
class UserController extends Controller
{
public function index()
{
Route::post('/login',function(){
$cred = Input::only('username','password');
if(Auth::attempt($cred)){
return Redirect::intended('/');
}else{
$error = "Username or password is incorrect.";
}
});
}
现在在routes.php
:
Route::get('index', ['middleware' => 'auth', 'uses' => 'UserController@index']);
但是,以下代码会引发一个错误:
MethodNotAllowedHttpException in RouteCollection.php line 201:
发布于 2015-08-06 09:44:54
在你的路线上这样做怎么样?
Route::post('/login', ['uses' => 'UserController@index']);
在您的控制器中,您可以进行验证。
public function index()
{
$cred = Input::only('username','password');
if(Auth::attempt($cred)){
return Redirect::intended('/');
}else{
$error = "Username or password is incorrect.";
}
}
https://stackoverflow.com/questions/31851506
复制相似问题