如何使用已存储为数组的变量重定向

时间:2015-07-03 13:52:21

标签: php arrays laravel laravel-4

我有以下代码:

public function searchgrouppost(){

         $subject1 = Input::get('subject1');
         $subject2 = Input::get('subject2');
         $subject3 = Input::get('subject3');
         $subject4 = Input::get('subject4');
         $subject5 = Input::get('subject5');
         $subject6 = Input::get('subject6');
         $subject7 = Input::get('subject7');

//collects by variables and stores them in an array                              
$subjects = [];
$inputs = Input::all();
array_walk($inputs, function ($v, $k) use (&$subjects) {
  if(starts_with($k, 'subject')) {
    $subjects[$k] = $v;
   }
});
  //counts the number of subjects that are chosen    
 $index = count($subjects);


if  ($index == 1){

 //the code below is wrong because if i click on any 
 //other subject other than subject1 I get an error. 

return Redirect::route('home', array('subject1' => $subject1, 'subject2' =>
 $subject2, 'subject3' => $subject3, 'subject4' => $subject4,
'subject5' => $subject5, 'subject6' => $subject6, 'subject7' => $subject7));  

//if the user chooses two subjects

elseif ($index == 2){
//code below is wrong 
return Redirect::route('hometwo', array('subject1' => $subject1,
'subject2'   => $subject2, 'subject3' => $subject3, 
'subject4' =>  $subject4,'subject5' => $subject5, 'subject6' => $subject6,
 'subject7' => $subject7));  
} 

我正在尝试将用户重定向到我可以执行的相应网址但是如何传递变量以便我可以在另一个网页上使用该值?

我无法通过$subjects,因为它是一个数组。

如果用户选择一个主题,我的路线代码如下:

Route::get('/home/{subject}', array( 'as' => 'home', 
'uses' => 'GroupSubjectController@home'));

1 个答案:

答案 0 :(得分:0)

使用session flash,这会在会话中放置一个变量,但仅限于一个请求。

在您进行重定向之前,添加以下行:

\Session::flash('subjects', $subjects);

现在,在新页面上,您可以使用以下命令找到该数组:

\Session::get('subjects');