Skip to main content

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.

How do I install the bundle?​

composer require ideal-postcodes/symfony-bundle

Symfony Flex registers the bundle. Without Flex, add it to config/bundles.php:

config/bundles.phpphp
return [
// ...
IdealPostcodes\Symfony\IdealPostcodesSymfonyBundle::class => ['all' => true],
];

How do I configure it?​

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.

config/packages/ideal_postcodes.yamlyaml
ideal_postcodes:
api_key: '%env(IDPC_API_KEY)%'
management_key: '%env(IDPC_MANAGEMENT_KEY)%'
.env.localbash
IDPC_API_KEY=ak_test
IDPC_MANAGEMENT_KEY=uk_...
OptionPurpose
api_keyRequired. Your API key, or a licensee key beginning sl_
management_keyYour Management Key for key, licensee and configuration operations. Leave it unset elsewhere
base_uriDefaults to https://api.ideal-postcodes.co.uk/v1
http_clientService id of a PSR-18 client to send requests with. See below for the default
headersHeaders sent with every request. A User-Agent naming your application is sent ahead of the bundle and SDK identifiers

How do I use the client?​

Type-hint IdealPostcodes\Client. It is a private, autowirable service, also under IdealPostcodes\Contracts\ClientContract.

src/Controller/AddressController.phpphp
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.

Which HTTP client sends the requests?​

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/psr7

Timeouts, retries and proxies then come from framework.http_client:

config/packages/framework.yamlyaml
framework:
http_client:
default_options:
timeout: 5

Without 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.

How do I test code that uses the SDK?​

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-client
config/services_test.yamlyaml
services:
Http\Mock\Client: ~

ideal_postcodes:
api_key: ak_test
http_client: Http\Mock\Client
tests/AddressControllerTest.phpphp
$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.