Search code examples
symfonysymfony-2.7

How to send an Array by Response/Request?


I have the following Question:

I use a Controller with this two functions:

Both functions has an own route @see routing

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

}

In the function indexAction i should forward to the next function (resultAction), because i need a new URL. As well i need the Array $data which is generated in the function indexAction, in the function resultAction, but i don't know, how i can call a route with an Array as parameter.

The routing file looks so:

dataHome:
pattern:  /home
defaults: { _controller: "GeneralCommonBundle:DataHome:index"}

result:
pattern:  /{limit}/{offset}/result
defaults: { _controller: "GeneralCommonBundle:DataHome:result", limit: 20, offset: 0 }

I tried to use a global variable (i know it's not a nice paradigma) because the functions are in the same Controller, but it did not works. As well i tried to put the $data Array in the Response.. but it worked also not..

How i can call a route with an Array as parameter? Or store this Array temporary, that i can use it after the forwarding?

Thanks for your Support!!


Solution

  • You surely can send as parameter in the route, here is my quick test:

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

    But remember, that GET has some limitations for string length and I think that using session variable will be more appropriate, just set serialized data in indexAction and then get it in resultAction