dongpo9071
2014-12-29 07:31
浏览 42
已采纳

重定向循环错误laravel auth

hi im trying to make a good user auth form but i ve have had some problems , so first i tried this :

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');
    }
});

and this is the form blade page :

     {{ 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>

and it gave me the redirect loop error

and then i tried this :

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');
    }
});

and it gives me :

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)

does anyone knows whats the problem? first code should have worked perfectly but why redirect error?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongliushui2001 2014-12-29 07:43
    已采纳

    Firstfully, change your route names, to: Route::get('login', function(){}); And Route::get('sign-in', function(){});

    Get auth page:

    Route::get('login', function()
    {
       return View::make('your-auth-view');
    }
    

    And your sign-in handler should looks like:

    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');
        }
    });
    

    And form will change to:

    {{ Form::open(array('url' => 'sign-in', 'class' => 'form-horizontal')) }}
    

    P.S. You have a redirect loop, because, you have a two the same routes, and when you submit the form you redirect to login page again and again

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题