Installation Add the bundle via Composer:
composer require avro/location-bundle
Enable it in config/bundles.php:
return [
// ...
Avro\LocationBundle\AvroLocationBundle::class => ['all' => true],
];
First Use Case Fetch a list of US states in a controller:
use Avro\LocationBundle\Service\LocationService;
class LocationController extends AbstractController
{
public function index(LocationService $locationService)
{
$states = $locationService->getStates('US');
return $this->json($states);
}
}
Where to Look First
src/Service/LocationService.php for core functionality.Resources/data/ for raw location data (CSV/JSON).Twig/ for template helpers (if available).Fetching Locations
// Get all countries
$countries = $locationService->getCountries();
// Get Canadian provinces
$provinces = $locationService->getProvinces('CA');
// Get cities in a state (e.g., California)
$cities = $locationService->getCities('US', 'CA');
Integration with Forms Use in Symfony Forms for address validation:
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
$builder->add('state', ChoiceType::class, [
'choices' => $locationService->getStates('US'),
'choice_label' => 'name',
'choice_value' => 'abbreviation',
]);
Caching Data Wrap service calls in a cache layer (e.g., Symfony Cache component):
$cache = $this->get('cache.app');
$key = 'locations:states:US';
$states = $cache->get($key, function() use ($locationService) {
return $locationService->getStates('US');
});
Custom Data Sources
Extend the bundle by adding new data files to Resources/data/ and update the service to parse them.
LocationService is autowireable (no manual DI config needed).config/packages/avro_location.yaml for bundle-specific settings (if any).Data Accuracy
Performance
$locationService->getCities('US', 'CA', 0, 10); // Limit to 10 cities
Missing Features
Bundle Maturity
Inspect Data Structure Dump raw data to understand the format:
dd($locationService->getCountries());
Example output may look like:
[
['code' => 'US', 'name' => 'United States', 'abbreviation' => 'USA'],
['code' => 'CA', 'name' => 'Canada', 'abbreviation' => 'CAN'],
]
Override Data Files
Copy Resources/data/ to your project and extend/modify files (e.g., states.csv). The bundle likely loads from these paths by default.
Logging Add debug logs to track data loading:
$this->get('logger')->debug('Loaded states for country: ' . $countryCode);
Custom Location Types Extend the service to support new location types (e.g., counties, postal codes):
// In a custom service
public function getCounties(string $stateCode): array
{
return $this->loadData('counties_' . $stateCode . '.json');
}
Database Backend
Replace the file-based data source with a database table. Override the loadData() method in a subclass of LocationService.
Twig Filters Add custom Twig filters for templating:
// In a compiler pass or Twig extension
$twig->addFilter(new \Twig\TwigFilter('format_location', [$locationService, 'formatLocation']));
Usage in Twig:
{{ city|format_location }}
API Integration Cache API responses (e.g., from GeoNames) and fall back to the bundle’s data:
try {
$data = $this->fetchFromGeoNamesApi();
} catch (\Exception $e) {
$data = $locationService->getFallbackData();
}
How can I help you explore Laravel packages today?