我有以下问题:
我使用具有以下两个功能的控制器:
两种功能都有自己的路线@see路线
public function indexAction(Request $request)
{
$searchForm = $this->getSearchForm();
$searchForm->handleRequest($request);
**$data** = $searchForm->getData();
if($searchForm->isValid()){
if(!$data['birthdate'] && !$data['birthyear'] && !$data['patientID'] && !$data['patientNO']){
$searchForm->addError(new FormError("Please specify at least one search parameter."));
}
else
{
return $this->forward('GeneralCommonBundle:DataHome:result', array(
'limit' => '20',
'offset' => '0'
));
//return $this->redirect($this->generateUrl('result', array('limit' => '20', 'offset' => '0')));
}
}
. . . . .
}
public function resultAction(Request $request, $limit, $offset){
$repo = $this->getDoctrine()->getManager()->getRepository('DataLiveBundle:DataAPatient');
$qb = $repo->getFindingPatientQuery($data['birthdate'],
$data['patientID'],$data['birthyear'] ,$data['patientNO'], $data['center'], $data['registry'] ,$data['study']);
$total = $repo->countQueryResults($qb);
$qb = $repo->addLimitToQuery($qb, $limit, $offset);
$paginationOptions = array(
'total' => $total,
'limit' => $limit,
'offset' => $offset
);
//$query = $qb->getQuery();
$entities = $repo->getResults($qb);
return $this->render('GeneralCommonBundle:DataHome:show.html.twig', array(
'records'=> $entities,
'isNew' => false,
'paginationOptions' => $paginationOptions,
'newrecord' => false,
'birthdate'=> $data['birthdate'],
'patientID'=> $data['patientID'],
'birthyear'=> $data['birthyear'],
'patientNO'=> $data['patientNO'],
'center' => $data['center'],
'registry' => $data['registry'],
'study' => $data['study']
));
}
在函数indexAction中,我应该转发到下一个函数(resultAction),因为我需要一个新的URL。以及我需要的数组$的数据,其产生的作用的indexAction,在功能resultAction,但我不知道,我怎么可以调用路由与数组作为参数。
路由文件如下所示:
dataHome:
pattern: /home
defaults: { _controller: "GeneralCommonBundle:DataHome:index"}
result:
pattern: /{limit}/{offset}/result
defaults: { _controller: "GeneralCommonBundle:DataHome:result", limit: 20, offset: 0 }
我试图使用全局变量(我知道这不是一个很好的范式),因为这些函数位于同一Controller中,但没有用。同样,我尝试将$ data数组放入Response ..但它也没有起作用。
如何使用数组作为参数调用路由?还是存储此Array临时文件,以便在转发后可以使用它?
谢谢您的支持!!
您当然可以在路由中作为参数发送,这是我的快速测试:
public function testAction()
{
$testArray = [
'data1' => 'data',
'data2' => 'data'
];
return $this->forward('App\Controller\TestController::otherAction', [
'data' => serialize($testArray)
]);
}
public function otherAction($data)
{
$data = unserialize($data);
return new JsonResponse($data);
}
但是请记住,GET对于字符串长度有一些限制,我认为使用会话变量会更合适,只需在indexAction中设置序列化的数据,然后在resultAction中获取它
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句