Symfony
ideal-postcodes/symfony-bundle configures the PHP SDK from your application config and lets you inject the client into a controller or service. It supports Symfony 6.4, 7 and 8 on PHP 8.2 or later.
Install
composer require ideal-postcodes/symfony-bundleSymfony Flex registers the bundle. Without Flex, add it to config/bundles.php:
return [
// ...
IdealPostcodes\Symfony\IdealPostcodesSymfonyBundle::class => ['all' => true],
];Configuration
Set your API key, and the Management Key if you manage keys, licensees or configurations from this application. Both belong in environment variables, never in a committed file.
ideal_postcodes:
api_key: '%env(IDPC_API_KEY)%'
management_key: '%env(IDPC_MANAGEMENT_KEY)%'IDPC_API_KEY=ak_test
IDPC_MANAGEMENT_KEY=uk_...| Option | Purpose |
|---|---|
api_key | Required. Your API key, or a licensee key beginning sl_ |
management_key | Your Management Key for key, licensee and configuration operations. Leave it unset elsewhere |
base_uri | Defaults to https://api.ideal-postcodes.co.uk/v1 |
http_client | Service id of a PSR-18 client to send requests with. See below for the default |
headers | Headers sent with every request. A User-Agent naming your application is sent ahead of the bundle and SDK identifiers |
Usage
Type-hint IdealPostcodes\Client. It is a private, autowirable service, also under IdealPostcodes\Contracts\ClientContract.
use IdealPostcodes\Client;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
final class AddressController
{
public function __construct(private readonly Client $client) {}
#[Route('/addresses/{postcode}')]
public function byPostcode(string $postcode): JsonResponse
{
$lookup = $this->client->lookup($postcode);
return new JsonResponse([
'found' => $lookup->found,
'addresses' => $lookup->addresses,
'suggestions' => $lookup->suggestions,
]);
}
#[Route('/suggest')]
public function suggest(string $query): JsonResponse
{
return new JsonResponse($this->client->findAddress(['query' => $query])->result->hits);
}
}Response objects are JsonSerializable, so JsonResponse renders them with the API's field names.
Each API operation is one method on the client, such as addressCleanse() or keyUsage(). The endpoints reference lists them all. The service id is ideal_postcodes.client.
HTTP client
The bundle uses the Psr\Http\Client\ClientInterface service when your application has one. Symfony's HttpClient component registers it once a PSR-17 factory is installed:
composer require symfony/http-client nyholm/psr7Timeouts, retries and proxies then come from framework.http_client:
framework:
http_client:
default_options:
timeout: 5Without that service, the SDK discovers any installed PSR-18 client, such as Guzzle. To use a specific client, set http_client to its service id.
Testing
Replace the HTTP client in the test environment with a mock that answers from fixtures. php-http/mock-client is a PSR-18 client that returns queued responses.
composer require --dev php-http/mock-clientservices:
Http\Mock\Client: ~
ideal_postcodes:
api_key: ak_test
http_client: Http\Mock\Client$mock = static::getContainer()->get(Http\Mock\Client::class);
$mock->addResponse(new Nyholm\Psr7\Response(200, ['Content-Type' => 'application/json'], $postcodeBody));
$client->request('GET', '/addresses/BR8 7RE');Errors are the SDK's own exceptions. See error handling.