我正在尝试使用laravel创建一个登录面板,并且发现很难理解为什么我的页面没有重定向到任何页面
这是我的routes.php
的内容 Route::get('/myapp/', 'MyApp@index');
//route to show the login form
Route::get('/myapp/login', 'MyApp@login');
//route to process the login form
Route::post('/myapp/login', 'MyApp@doLogin');
//route to show the registration form
Route::get('/myapp/register', 'MyApp@register');
//route to process the registration form
Route::post('/myapp/register', 'MyApp@saveRegister');
//route to show the reset password page
Route::get('/myapp/resetpassword','MyApp@resetpassword');
//route to process the password reset request
Route::post('/myapp/resetpassword', 'MyApp@doReset');
以下是控制器的完整代码
<?php
class MyApp extends BaseController
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('myapp.index');
}
public function register()
{
return View::make('myapp.register');
}
/**
* Insert the data into database.
*
* @return Response
*/
public function saveRegister()
{
$input = Input::all();
//validate the form data provided by the user
$rules = array(
'username' => 'required|alphaNum|min:6',
'password' => 'required|alphaNum|min:6',
'email' => 'required|email',
'phone' => 'required',
);
//now validate the above rules
$validator = Validator::make($input,$rules);
//If validator fails send user back to registration form
if($validator->fails())
{
return Redirect::back()//if validation fails redirect back to registration page
->withErrors($validator)//send back all the errors to the form
->withInput(Input::except('password')//send all data back to form except password
);
}
else
{
$password = $input['password'];
$password = Hash::make($password);
$myapp = new User;
$myapp->username = $input['username'];
$myapp->password = $password;
$myapp->email = $input['email'];
$myapp->phone = $input['phone'];
$myapp->save();
return Redirect::to('myapp')->with('success','Registration successful');
}
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function login()
{
return View::make('myapp.login');
}
public function doLogin()
{
$input = Input::all();
$rules = array(
'username' => 'required',
'password' => 'required'
);
//ow validate the rules
$validator = Validator::make($input,$rules);
//if validator passed
if($validator->fails())
{
return Redirect::back()//if validation fails redirect back to registration page
->withErrors($validator)//send back all the errors to the form
->withInput(Input::except('password')//send all data back to form except password
);
}
else
{
$username = Input::get('username');
$password = Hash::make(Input::get('password'));
$userdata = array(
'username' => $username,
'password' => $password
);
//Attempt to do the login
if($entry=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)
return Redirect::to('myapp')->with('message','Success');
}
else
{
return Redirect::to('myapp/fail');
/*return Redirect::back()
->withErrors('message','Wrong username or password')
->withInput(Input::except('password'));*/
}
}
}
/**
* Display the reset password page.
*
* @return Response
*/
public function resetpassword()
{
return View::make('myapp.resetpassword');
}
public function doReset()
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
现在我的页面重定向到myapp / fail,这意味着它无法使用数据表验证数据
答案 0 :(得分:1)
在其他部分 //如果验证确定
$username = Input::get('username');
$password = Input::get('password');
$userdata = array(
'username' => $username,
'password' => $password
);
if(Auth::attempt($userdata)) ....
删除Hash::make
部分并获取密码$password = Input::get('password');
答案 1 :(得分:0)
我遇到的问题是,即使我提供的数据是正确的,我的表单也没有重定向到所需的页面。正如你在MyApp控制器中看到的那样,我有一个名为saveRegister的函数,它在散列我在表单中提供的密码后将数据插入到users表中
$password = Hash::make($password);
现在在我的doLogin函数中,我正在做的是散列密码来验证,即使laravel的Auth函数自己散列密码,所以我不需要手动散列密码以使其与数据库匹配。