Created
September 8, 2015 01:44
-
-
Save miguelplazasr/4ef0729df507a01db35a to your computer and use it in GitHub Desktop.
Ajax Controller for a country, state and city selects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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