简体   繁体   中英

Ajax and store session in Laravel

I Like to store a id in session using AJAX in laravel,In normal php and jQuery i am using the following method ,& how to attain the same in laravel

Normal Method:

$('.action').click(function(){
    var editaction=($(this).attr("id"));
 $.ajax({
    type:"POST",
      url:"test_session.php",
      data:"test_id="+editaction,
      success:function(results){
        window.location.href="newredirect.php"
      }
    }); 
});

You can use same js code for Laravel. But you need to change "test_session.php" with your route url and "newredirect.php" with redirect route url.

Example Code:

$('.action').click(function(){
    var editaction=($(this).attr("id"));
    $.ajax({
        type:"POST",
        url:"ROUTE URL",
        data:"test_id="+editaction,
        success:function(results){
            window.location.href="REDIRECTED ROUTE URL"
        }
    }); 
});

Here is example how to save session in Laravel Controller:

public function ExampleRoute(Request $request){
    $username = Input::get('username');
    if($username){
        $request->session()->put('username', $username);
        return 'success';
    }
}

If you want to get your session, you can use that controller example:

public function ExampleRouteOfGetSession(Request $request){
    $username = $request->session()->get('username');
    return $username;
}

You can see Laravel Documentation for more information - https://laravel.com/docs/5.4/session#using-the-session

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM