Skip to main content

Symfony

addresszen/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 addresszen/symfony-bundle

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

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

Configure​

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/addresszen.yamlyaml
addresszen:
api_key: '%env(ADDRESSZEN_API_KEY)%'
management_key: '%env(ADDRESSZEN_MANAGEMENT_KEY)%'
.env.localbash
ADDRESSZEN_API_KEY=ak_test
ADDRESSZEN_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.addresszen.com/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

Use the client​

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

src/Controller/AddressController.phpphp
use AddressZen\Client;
use AddressZen\Types\UsaVerifyMatch;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\Routing\Attribute\Route;

final class AddressController
{
public function __construct(private readonly Client $client) {}

#[Route('/verify')]
public function verify(#[MapQueryParameter] string $address): JsonResponse
{
$result = $this->client->addressVerify(['query' => $address])->result;

return new JsonResponse([
'verified' => $result instanceof UsaVerifyMatch,
'result' => $result,
]);
}

#[Route('/suggest')]
public function suggest(#[MapQueryParameter] 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 addressVerify() or keyUsage(). The endpoints reference lists them all. The service id is addresszen.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/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.

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: ~

addresszen:
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'], $verifyBody));

$client->request('GET', '/verify?address=1010 Cauthen Ln, Alamogordo, NM 88310');

Errors are the SDK's own exceptions. See error handling.