我有以下功能:
public function searchsubject(){
$subject1 = Input::get('subject1');
$subject2 = Input::get('subject2');
$subject3 = Input::get('subject3');
$subject4 = Input::get('subject4');
//if a single subject is chosen
return Redirect::route('home', array('subject1' => $subject1, 'subject2' =>
$subject2, 'subject' => $subject3, 'subject' => $subject4));
//if two subjects are chosen
return Redirect::route('hometwo', array('subject1' => $subject1, 'subject2'
=> $subject2, 'subject' => $subject3, 'subject' => $subject4));
//if three subjects are chosen
return Redirect::route('homethree', array('subject1' => $subject1, 'subject2' =>
$subject2, 'subject' => $subject3, 'subject' => $subject4));
我的目标是将用户重定向到三个网址中的任何一个。
问题是如何实现这一目标?
我的想法是将所有这些值打包到一个数组中并检查它们中有多少具有值并相应地重定向但这是如何完成的?
答案 0 :(得分:0)
你可以尝试这样的事情,首先得到主题:
$subjects = [];
$inputs = Input::all();
array_walk($inputs, function ($v, $k) use (&$subjects) {
if(starts_with($k, 'subject')) {
$subjects[$k] = $v;
}
});
现在您拥有以inputs/fields
开头的所有subject
,例如:subject1
,subject2
等等。现在,只需获得$subjects
的计数:
$index = count($subjects);
现在您可以使用,例如:
// Keep this array pre-defined, could be a class property, if so then use $this->urls
$urls = ['zero', 'url-for-one-subject', 'url-for-two-subjects', 'url-for-three-subjects'];
// Redirect now using $urls[$index]
return Redirect::route($urls[$index)->with($subjects);