dtfo55908
2017-04-25 13:04 阅读 65

当会话无效时登录成功时,laravel从Controller重定向到View

I'm new to Laravel 5.2 . I have been trying to create a login Page using Session and i wanted to redirect to another page when user login is successfull ...I validate users in Controller create Session and Redirect to a Success Page View but its not returning ...i tried to solve it but i am unable to do so ...the Controller is not returning the View success page ..am i doing it correctly or im missing something

<form id="loginForm">
  <div class="container">
    <label><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="username" id="username" required>

    <label><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="password" id="password" required>

    <button type="button" id="loginButton">Login</button>
    <input type="checkbox" checked="checked"> Remember me
  </div>

  <div class="container" style="background-color:#f1f1f1">
    <button type="button" class="cancelbtn">Cancel</button>
    <span class="psw">Forgot <a href="#">password?</a></span>
  </div>
</form>

</body>
<script type="text/javascript">
    $(function(){
        $('#loginButton').click(function(){
            //alert(SERVER_NAME);
            var formData = new FormData($('#loginForm')[0]);

            $.ajax({
              url : SERVER_NAME+"/api/userLogin",
              type :"POST",
              data :formData,
              processData: false,
              contentType: false,
              dataType :"json",
              beforeSend: function(){

              },               
              success : function(response)
              {

                console.log(response);
                //return false;
                  if(response.status==1){

                      alert(response.message);
                      // window.location.href = 'home';

                  }else{
                      alert(response.message);
                  }


              },
             complete:function(){


             }

            });  
        });

    });

</script>`

MY Controller

 public function userLogin(Request $request)
{
    $input = $request->all();
    //return $input;
    $userName=$input['username'];
    $password=$input['password'];
    //return $userName;
    $results = wowza::authenticate($userName,$password); 
    if (sizeof($results)>0){

         if($results[0]->email==$userName && $results[0]->password == $password)
            {
                Session::set('variableName', $userName);

                $results['status']=1;
                return \View::make('user.home')->with('results',$results);
                // return response()->json($results);
            }
    }

    return sizeof($results);

}

My Success Page

{{Session::get('variableName')}}

My Route

    Route::get('/', function () {
    return view('user.login');
});

Route::get('home', function () {
    //Session::get('username');
    return view('user.home');
});
$api->version('v1',function($api){
$api->post('userLogin','App\Http\Controllers\Api\V1\wowzaController@userLogin');
});
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • duanliu6083 duanliu6083 2017-04-26 10:19

    You can change at auth controller as protected $redirectTo = '/dashboard'; or at route as Route::get('dashboard', function () { return redirect('your-path'); }); `

    点赞 评论 复制链接分享
  • douweng7083 douweng7083 2017-04-26 11:06

    Your page is not redirecting because you are loading the view file in your controller. Instead of this code:

    return \View::make('user.home')->with('results',$results);

    You should use redirect('home') method.

    Let me know if that does not work.

    点赞 评论 复制链接分享

相关推荐