Installation
composer require common-gateway/zgw-bundle
Add to config/bundles.php:
return [
// ...
CommonGateway\ZGWBundle\ZGWBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console config:dump-reference CommonGateway\ZGWBundle\Resources/config/zgw.yaml
Configure in config/packages/zgw.yaml:
zgw:
api_version: '1.5.1'
base_uri: '%env(ZGW_BASE_URI)%'
client_id: '%env(ZGW_CLIENT_ID)%'
client_secret: '%env(ZGW_CLIENT_SECRET)%'
First Use Case Fetch a case (zaak) via the API:
use CommonGateway\ZGWBundle\Client\ZGWClient;
$client = $this->container->get(ZGWClient::class);
$case = $client->getZaak('urn:uuid:123e4567-e89b-12d3-a456-426614174000');
INSTALLATION.md and the GitHub Wiki (if available).php bin/console list zgw to see available commands.src/Entity/ for default models (e.g., Zaak, ZaakType, Document).API Client Integration
Use the ZGWClient to interact with ZGW APIs:
// Fetch a case with minimal data (filtering)
$case = $client->getZaak('urn:uuid:...', ['fields' => ['zaaknummer', 'omschrijving']]);
// Create a case
$newCase = $client->createZaak([
'zaaktype' => 'urn:uuid:zaaktype-uuid',
'omschrijving' => 'New case description',
]);
Event-Driven Extensions
Extend default entities via the extend pattern:
// In a service or bundle
$zaak = $client->getZaak('urn:uuid:...');
$zaak->extend('custom_field', 'value'); // Add custom data
$zaak->save(); // Persist extended data (if using Doctrine)
Filtering and Minimalization Leverage filtering to reduce payload size:
// Fetch only required fields for a case type
$caseType = $client->getZaakType('urn:uuid:zaaktype-uuid', [
'fields' => ['omschrijving', 'vertrouwelijkheidaanduiding'],
]);
Authentication Handle OAuth2 via the bundle’s built-in client:
# config/packages/zgw.yaml
zgw:
auth:
token_url: '%env(ZGW_TOKEN_URL)%'
scopes: ['read', 'write']
# config/packages/doctrine.yaml
orm:
mappings:
ZGW:
type: attribute
dir: '%kernel.project_dir%/vendor/common-gateway/zgw-bundle/src/Entity'
prefix: 'CommonGateway\ZGWBundle\Entity'
alias: ZGW
api_version in config to avoid breaking changes.API Version Lock-In
1.5.1. Ensure your code doesn’t assume newer ZGW features unless explicitly configured.zgw.yaml and test upgrades carefully.Caching Headaches
php bin/console cache:clear
dev environment:
zgw:
client:
cache_enabled: '%kernel.debug%'
Extended Data Persistence
$zaak->extend()) is not automatically persisted unless configured.Rate Limiting
429 Too Many Requests gracefully:
try {
$client->getZaak('urn:uuid:...');
} catch (\CommonGateway\ZGWBundle\Exception\RateLimitExceededException $e) {
sleep($e->getRetryAfter());
retry();
}
UUID Handling
urn:uuid:...). Validate UUIDs before passing them to the client:
if (!\Ramsey\Uuid\Uuid::isValid($urn)) {
throw new \InvalidArgumentException('Invalid URN format');
}
Enable Verbose Logging
zgw:
client:
debug: true
Logs will appear in var/log/dev.log.
Inspect Raw Responses
Use the ZGWClient with debug: true to dump raw API responses:
$client->setDebug(true);
$response = $client->getZaak('urn:uuid:...');
Custom Entity Mappers Override default entity mappings by creating a custom mapper:
// src/Service/CustomZaakMapper.php
use CommonGateway\ZGWBundle\Mapper\ZaakMapper;
class CustomZaakMapper extends ZaakMapper {
public function map(array $data): Zaak {
$zaak = parent::map($data);
$zaak->setCustomField($data['custom_field'] ?? null);
return $zaak;
}
}
Register it in services.yaml:
services:
CommonGateway\ZGWBundle\Mapper\ZaakMapper:
alias: App\Service\CustomZaakMapper
Event Subscribers
Listen to ZGW events (e.g., zgw.zaak.created):
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use CommonGateway\ZGWBundle\Event\ZaakEvent;
class ZaakSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
ZaakEvent::CREATED => 'onZaakCreated',
];
}
public function onZaakCreated(ZaakEvent $event) {
// Handle new case
}
}
Custom API Endpoints Extend the client to support non-standard ZGW endpoints:
// src/Client/CustomZGWClient.php
use CommonGateway\ZGWBundle\Client\ZGWClient;
class CustomZGWClient extends ZGWClient {
public function getCustomEndpoint(string $path, array $data = []) {
return $this->request('GET', "/custom/{$path}", $data);
}
}
Base URI Trailing Slash
Ensure base_uri in zgw.yaml does not end with a slash:
zgw:
base_uri: 'https://api.example.com' # No trailing slash!
Environment Variables
Use env() in config for sensitive data:
zgw:
client_id: '%env(ZGW_CLIENT_ID)%'
client_secret: '%env(ZGW_CLIENT_SECRET)%'
Never hardcode secrets in zgw.yaml.
Default Fields The bundle includes default fields for entities. To exclude them, use:
$client->getZaak('urn:uuid:...', ['fields' => []]); // Empty array = no fields
How can I help you explore Laravel packages today?