Laravel session data get cleared out after redirec

2019-08-03 08:48发布

Using Laravel 5.2

Im trying to store session data in controller, then if validation succeed, i want to redirect the user to next page. When i do the Redirct::to('nextpage') the session data is lost in my next page and controller.

This is the controller in getting the form post value from 'domainSearch'

class domainSearch extends Controller
{ 
    public function store(Request $request)
    {
        // validate the info, create rules for the inputs
        $rules = array(
            'domainSearch'    => 'required' // make sure the username field is not empty
        );

        // run the validation rules on the inputs from the form
        $validator = Validator::make(Input::all(), $rules);

        // if the validator fails, redirect back to the form
        if ($validator->fails()) {
            return Redirect::to('/')
                ->withErrors($validator) // send back all errors to the login form
                ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
        }
            else {
                // validation not successful, send back to form
            $request->session()->put('domainname', $request->get('domainSearch'));
                return Redirect::to('/domainlist');

            }

        }
    }

This is the controller im trying to pass session data to and pick up. But it always remain null.

 class DomainList extends Controller
    {
        public function index(Request $request){

            var_dump($request->get('domainname'));
            return view('domainlist');
        }
    }

If i send the user to next page with return view('view'); the session data is there, but get cleared out when using the redirect function.

How to i redirect without losing session data and variables?

2条回答
该账号已被封号
2楼-- · 2019-08-03 08:58

try var_dump($request->session()->get('domainname')); in your index.

BTW, you can also use the session() global function to store or retrieve your session data.

Route::get('home', function () {
  // Retrieve a piece of data from the session...
  $value = session('key');

  // Store a piece of data in the session...
  session(['key' => 'value']);
});
查看更多
Explosion°爆炸
3楼-- · 2019-08-03 09:08

It happens because you try to retrieve GET parameter instead of SESSION variable. Try this:

 class DomainList extends Controller
{
    public function index(Request $request){

        var_dump($request->session()->get('domainname'));
        return view('domainlist');
    }
}
查看更多
登录 后发表回答