[ad_1]
I am creating a Symphony application that browses different data sources.
the controller that I created knows too much about the data source but the application is designed in a way to not expect that.
The data source could be DB, JSON or XML.
is there any way to implement interfaces to do that?
My controller knows the location of the XML file, and browse different data seperatly. I want to do it in one action.
That's my current controller ;
public function searchAction(Request $request)
if ($request->getMethod() == 'POST')
$search_for = $request->get('search');
//getting the searched products from the database
$repository = $this->getDoctrine()->getRepository('TyreTyreBundle:Products');
$query = $repository->createQueryBuilder('u')
->where("u.name LIKE '%".$search_for."%' or u.manufacturer LIKE '%".$search_for."%'")
->getQuery();
$results = $query->getResult();
//adding the XML file products
$file_url = "bundles/tyretyre/xml/products.xml";
//Convert the products.XML file into a SimpleXMLElement object
$simpleXMLElementObject = simplexml_load_file($file_url);
$i=0;
//the array where will saved the searched products from the XML file
$xml_result = [];
//looping the xml object to find matching results
while ($simpleXMLElementObject->product[$i])
//first we will convert to lower case both searched item and the tested name
if (strstr(strtolower($simpleXMLElementObject->product[$i]->name),strtolower($search_for)))
//push that element into the array to display it later in the twig file
array_push($xml_result, $simpleXMLElementObject->product[$i]);
$i++;
//end of products searching from the XML source
//display the detail page with passing the DB result and XML result arrays
return $this->render('TyreTyreBundle:Default:detail.html.twig', array('results' => $results,'xml_result' => $xml_result));
return $this->render('TyreTyreBundle:Default:search.html.twig');
[ad_2]
لینک منبع