Skip to content

Instantly share code, notes, and snippets.

@miguelplazasr
Created September 8, 2015 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelplazasr/4ef0729df507a01db35a to your computer and use it in GitHub Desktop.
Save miguelplazasr/4ef0729df507a01db35a to your computer and use it in GitHub Desktop.
Ajax Controller for a country, state and city selects
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* Ajax controller
*
* @Route("/ajax")
*/
class AjaxController extends Controller {
/**
* @Route("/states", name="select_states")
*/
public function statesAction(Request $request) {
$country_id = $request->request->get('country_id');
$em = $this->getDoctrine()->getManager();
$states = $em->getRepository('AppBundle:State')->findBy(array(
'country' => $country_id
));
$states1 = $em->getRepository('AppBundle:State')->findByCountryId($country_id);
return new JsonResponse($states1);
}
/**
* @Route("/cities", name="select_cities")
*/
public function citiesAction(Request $request) {
$state_id = $request->request->get('state_id');
$em = $this->getDoctrine()->getManager();
$cities = $em->getRepository('AppBundle:City')->findByStateId($state_id);
return new JsonResponse($cities);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment