Installation
Run composer require chaplean/location-bundle in your Symfony 2.8+ project.
Register the bundle in AppKernel.php:
new Chaplean\Bundle\LocationBundle\ChapleanLocationBundle(),
Load Default Data Populate cities via CLI:
php bin/console location:load:cities
(Note: Requires external data sources—see Resources for INSEE/Postal Code APIs.)
First Use Case Inject and query locations in a controller/service:
use Chaplean\Bundle\LocationBundle\Model\LocationInterface;
public function getCityByName(LocationInterface $locationManager)
{
$city = $locationManager->findCityByName('Paris');
return $json($city);
}
Data Loading
location:load:cities for one-time imports.Chaplean\Bundle\LocationBundle\Command\LoadCitiesCommand to fetch from private APIs.Dependency Injection
LocationInterface (e.g., CityManager, RegionManager) into services/controllers.# services.yaml
App\Service\LocationService:
arguments:
$regionManager: '@chaplean_location.region.manager'
Validation & Filtering
PostalCodeValidator) for form handling:
$validator = $this->get('validator');
$errors = $validator->validate($entity, ['Constraints\ValidPostalCode']);
API Integration
LocationApiClient (if extended):
$client = $this->container->get('chaplean_location.api_client');
$response = $client->getDepartments();
city, department, region. Align your Doctrine mappings:
# config/doctrine/orm.yml
Chaplean\Bundle\LocationBundle\Entity\City:
type: entity
table: city
translations/messages.fr.yml:
chaplean_location:
department: "Région administrative"
LocationManager to cache frequent queries (e.g., city lookups):
$cache = $this->container->get('cache.app');
$cachedCity = $cache->get("city_{$name}");
Outdated Data
php bin/console location:validate:cities
CronExpression.Missing Regions
Command Exit Codes
location:load:cities exits with 1 on failure (e.g., API downtime). Handle in migrations:
if ($exitCode !== 0) {
throw new \RuntimeException('Failed to load cities. Check logs.');
}
Doctrine Conflicts
config/packages/chaplean_location.yaml:
chaplean_location:
entity:
city: App\Entity\CustomCity
APP_DEBUG=1 php bin/console location:load:cities
$profiler = $this->container->get('profiler');
$profiler->collect();
Custom Data Sources
Chaplean\Bundle\LocationBundle\DataLoader\CityLoader to use a private database:
class CustomCityLoader extends CityLoader {
public function load() {
return $this->entityManager->getRepository(CustomCity::class)->findAll();
}
}
services:
chaplean_location.city_loader:
class: App\DataLoader\CustomCityLoader
tags: ['chaplean_location.city_loader']
Add New Location Types
LocationInterface and create a new manager (e.g., NeighborhoodManager):
class NeighborhoodManager implements LocationInterface {
public function findByPostalCode($code) { ... }
}
Resources/config/services.xml:
<service id="chaplean_location.neighborhood.manager" class="App\Manager\NeighborhoodManager" />
API Clients
LocationApiClient with a GraphQL/REST client:
class CustomApiClient implements LocationApiClientInterface {
public function getDepartments() {
return $this->httpClient->request('GET', 'https://api.example.com/departments');
}
}
fr) for INSEE data. Change in config/packages/chaplean_location.yaml:
chaplean_location:
locale: 'en' # For English labels
.env:
CHAPLEAN_LOCATION_API_KEY=your_key_here
How can I help you explore Laravel packages today?