Php Laravel会话数据在重定向到下一页后被清除

Php Laravel会话数据在重定向到下一页后被清除,php,laravel,redirect,laravel-5,frameworks,Php,Laravel,Redirect,Laravel 5,Frameworks,使用Laravel 5.2 我试图将会话数据存储在控制器中,如果验证成功,我想将用户重定向到下一页。当我执行Redirct::to('nextpage')时,会话数据在我的下一页和控制器中丢失 这是从“domainSearch”获取表单post值的控制器 class domainSearch extends Controller { public function store(Request $request) { // validate the info, c

使用Laravel 5.2

我试图将会话数据存储在控制器中,如果验证成功,我想将用户重定向到下一页。当我执行Redirct::to('nextpage')时,会话数据在我的下一页和控制器中丢失

这是从“domainSearch”获取表单post值的控制器

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

            }

        }
    }
这是我试图将会话数据传递给并拾取的控制器。但它始终保持为空

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

            var_dump($request->get('domainname'));
            return view('domainlist');
        }
    }
如果我用返回视图(“视图”)将用户发送到下一页;会话数据存在,但在使用重定向功能时会被清除


如何在不丢失会话数据和变量的情况下重定向?

之所以发生这种情况,是因为您试图检索GET参数而不是会话变量。试试这个:

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

        var_dump($request->session()->get('domainname'));
        return view('domainlist');
    }
}
尝试
var_dump($request->session()->get('domainname')在索引中

顺便说一句,您还可以使用
session()
global函数来存储或检索会话数据

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

使用
$request->session()->put
,然后使用
$request->get
。您可能需要使用
$request->session()->get
Excellent.)非常感谢!重定向到另一个函数或控制器时,“$value=session('key')”在Laravel 5.4中不起作用