---
title: "Symfony Integration"
description: "Wire file-backed catalogues, custom factories and translated exceptions with Symfony DependencyInjection"
type: "docs"
category: "doc"
tags: []
authors: [Anonymous]
date: "2026-10-20"
last_update: "2026-10-20"
time_minutes: 2
draft: false
unlisted: false
url: "https://www.derafu.dev/docs/data/repository/symfony"
---

# Symfony Integration

The repository package can be used directly or wired through Symfony DependencyInjection. The examples assume the application installs and configures that component.

## Configure Data Sources

```yaml
services:
    Derafu\Repository\Contract\RepositoryManagerInterface:
        class: Derafu\Repository\Service\RepositoryManager
        arguments:
            $dataProvider: '@Derafu\Repository\Service\DataProvider'

    Derafu\Repository\Service\DataProvider:
        arguments:
            $sources:
                App\Entity\Country: '%kernel.project_dir%/resources/data/countries.php'
                App\Contract\DocumentTypeInterface: '%kernel.project_dir%/resources/data/document_types.php'
            $config:
                normalization:
                    idAttribute: codigo
                    nameAttribute: glosa
```

`Country` and `DocumentType` are application entity classes. Request the repositories using the same identifiers used as source keys. The `DocumentTypeInterface` identifier follows the entity convention described in [entities and mapping](https://www.derafu.dev/docs/data/repository/entities). `%kernel.project_dir%` is supplied by the application environment.

An entity's `repositoryClass` attribute selects its specialized in-memory repository. No separate repository service is required for that construction path.

## Add a Factory

```yaml
services:
    App\Repository\ProductRepositoryFactory:
        arguments:
            $connection: '@app.database_connection'

    Derafu\Repository\Contract\RepositoryManagerInterface:
        class: Derafu\Repository\Service\RepositoryManager
        arguments:
            $dataProvider: '@Derafu\Repository\Service\DataProvider'
            $factories:
                App\Entity\Product: '@App\Repository\ProductRepositoryFactory'
```

This manager definition combines explicit factories with the data provider from the preceding example. `app.database_connection` is an application-defined service, and `ProductRepositoryFactory` implements the package's factory contract. The repository itself receives the dependencies its factory supplies.

For an application with only explicit factories, omit `$dataProvider`.

## Translated Exception Messages

The optional service resource registers `RepositoryTranslationResourceProvider` with the `derafu_translation.resource_provider` tag:

```yaml
imports:
    - { resource: '../vendor/derafu/repository/resources/config/repository-services.yaml' }
```

This import registers translation resources; it does not configure the manager or the data sources. The application's translator must consume the tagged resource providers as described in the [translation documentation](https://www.derafu.dev/docs/core/translation).

Without a service container, provide the resource provider directly:

```php
use Derafu\Repository\Translation\RepositoryTranslationResourceProvider;
use Derafu\Translation\TranslatorFactory;

$translator = TranslatorFactory::create(
    'es',
    resourceProviders: [new RepositoryTranslationResourceProvider()],
);
```

Package exceptions expose `trans($translator)` for translated output. Their original messages are in English, and the package includes a Spanish catalogue.



---
Last updated on 20/10/2026

