如果Auth::尝试成功,则无法重定向


Not able to redirect if Auth::attempt is successful

我试图用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,这意味着它无法使用数据表验证数据

在其他部分//如果验证正常

$username = Input::get('username');
$password = Input::get('password');
$userdata = array(
        'username'  => $username,
        'password'  => $password
);
if(Auth::attempt($userdata)) ....

删除Hash::make部分并将密码获取为$password = Input::get('password');

我有一个问题,为什么即使我提供的数据是正确的,我的表单也没有重定向到所需的页面。正如你在MyApp控制器中看到的那样,我有一个名为saveRegister的函数,它在散列了我以形式提供的密码后,将数据插入到用户表中

$password = Hash::make($password);

现在,在我的doLogin函数中,我所做的是对密码进行哈希验证,尽管laravel的Auth函数会自行对密码进行散列,所以我不需要手动对密码进行hash以将其与数据库匹配。