代码之家  ›  专栏  ›  技术社区  ›  helmi

如何通过响应/请求发送阵列?

  •  1
  • helmi  · 技术社区  · 7 年前

    我有以下问题:

    我使用具有以下两个功能的控制器:

    两种功能都有自己的路线 @请参见布线

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

    在函数索引操作中,我应该转发到下一个函数(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 }
    

    我尝试使用全局变量(我知道这不是一个好的范例),因为函数在同一个控制器中,但它不起作用。我还尝试将$数据数组放入响应中。。但它也起作用了。。

    如何使用数组作为参数调用路由? 或者将此数组临时存储,以便在转发后使用?

    感谢您的支持!!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Generwp    7 年前

    您当然可以在路由中作为参数发送,下面是我的快速测试:

    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中获取它