api-postcode/api-postcode-bundle
Installation:
composer require api-postcode/api-postcode-bundle
Ensure your composer.json meets the PHP version and Symfony requirements (≥7.1, Symfony 2.7–6.0).
Enable the Bundle:
Add to config/bundles.php (Symfony 4+):
return [
// ...
ApiPostcode\PostcodeBundle\ApiPostcodeBundle::class => ['all' => true],
];
Configure API Token:
Add to .env (recommended) or config/packages/api_postcode.yaml:
api_postcode:
token: '%env(API_POSTCODE_TOKEN)%'
First Use Case: Fetch an address in a controller or service:
use ApiPostcode\PostcodeBundle\Service\PostcodeService;
public function fetchAddress(PostcodeService $postcodeService)
{
$address = $postcodeService->fetchAddress('1012JS', 1);
return response()->json($address);
}
Service Integration:
Inject PostcodeService into controllers/services:
public function __construct(private PostcodeService $postcodeService) {}
API Endpoint: Use Symfony’s routing for direct API access:
# config/routes.yaml
api_postcode:
path: /api/postcode
controller: ApiPostcode\PostcodeBundle\Controller\PostcodeController::getAddress
methods: GET
Call with query params: /api/postcode?postcode=1012JS&nummer=1.
Batch Processing: Loop through postcodes (e.g., CSV import):
$postcodes = ['1012JS', '1013AA'];
foreach ($postcodes as $postcode) {
$address = $postcodeService->fetchAddress($postcode, 1);
// Process $address
}
Caching Responses: Cache results to reduce API calls (e.g., with Symfony Cache):
$cache = $this->container->get('api.postcode.cache');
$address = $cache->get($postcode, function() use ($postcode) {
return $this->postcodeService->fetchAddress($postcode, 1);
});
####?? regex) before API calls.try {
$address = $postcodeService->fetchAddress($postcode, $number);
} catch (\ApiPostcode\Exception\ApiException $e) {
// Log or retry
}
.env.Token Management:
config.yml violates security best practices. Always use .env.Rate Limits:
Deprecated Symfony Support:
House Number Handling:
fetchAddress() method expects house number as an integer (e.g., 1 not '1'). Non-integer inputs may fail silently.No Bulk Endpoint:
API_POSTCODE_DEBUG=true in .env to log raw API responses.GuzzleHttp client:
$client = $this->postcodeService->getClient();
$response = $client->send($request);
Custom Address Model:
Extend the Address class to add fields (e.g., getFullAddress()):
class CustomAddress extends \ApiPostcode\PostcodeBundle\Entity\Address {
public function getFullAddress() {
return "{$this->getStreet()} {$this->getHouseNumber()}, {$this->getZipCode()} {$this->getCity()}";
}
}
Override the service to return your model.
Add Geocoding: Use the latitude/longitude to reverse-geocode via Google Maps or OpenStreetMap:
$address = $postcodeService->fetchAddress('1012JS', 1);
$geocoded = $this->geocodingService->reverse($address->getLatitude(), $address->getLongitude());
Mock for Testing: Replace the service with a mock in tests:
$this->mockPostcodeService->shouldReceive('fetchAddress')
->once()
->andReturn(new Address('Dam', 'Amsterdam', 1, '1012JS', 4.4584, 52.2296));
Webhook Integration: Use the API’s webhook feature (if available) to push updates instead of polling.
1012JS = 1012js), but ensure consistency in your code.How can I help you explore Laravel packages today?