Using a Factory to Create Services

Using a Factory to Create Services

Symfony's Service Container provides a powerful way of controlling the creation of objects, allowing you to specify arguments passed to the constructor as well as calling methods and setting parameters. Sometimes, however, this will not provide you with everything you need to construct your objects. For this situation, you can use a factory to create the object and tell the service container to call a method on the factory rather than directly instantiating the class.

Suppose you have a factory that configures and returns a new NewsletterManager object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class NewsletterManagerFactory
{
    public static function createNewsletterManager()
    {
        $newsletterManager = new NewsletterManager();

        // ...

        return $newsletterManager;
    }
}

To make the NewsletterManager object available as a service, you can configure the service container to use the NewsletterManagerFactory::createNewsletterManager() factory method:

  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    services:
        app.newsletter_manager:
            class:   AppBundle\Email\NewsletterManager
            # call a static method
            factory: ['AppBundle\Email\NewsletterManager', create]
    
        app.newsletter_manager_factory:
            class: AppBundle\Email\NewsletterManagerFactory
    
        app.newsletter_manager:
            class:   AppBundle\Email\NewsletterManager
            # call a method on the specified service
            factory: ['@app.newsletter_manager_factory', createNewsletterManager]
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
            <service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
                <!-- call a static method -->
                <factory class="AppBundle\Email\NewsletterManager" method="create" />
            </service>
    
            <service id="app.newsletter_manager_factory"
                class="AppBundle\Email\NewsletterManagerFactory"
            />
    
            <service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
                <!-- call a method on the specified service -->
                <factory service="app.newsletter_manager_factory"
                    method="createNewsletterManager"
                />
            </service>
        </services>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    use Symfony\Component\DependencyInjection\Definition;
    // ...
    
    $definition = new Definition('AppBundle\Email\NewsletterManager');
    // call a static method
    $definition->setFactory(array('AppBundle\Email\NewsletterManager', 'create'));
    
    $container->setDefinition('app.newsletter_manager', $definition);
    
    $container->register('app.newsletter_manager_factory', 'AppBundle\Email\NewsletterManagerFactory');
    
    $newsletterManager = new Definition('AppBundle\Email\NewsletterManager');
    
    // call a method on the specified service
    $newsletterManager->setFactory(array(
        new Reference('app.newsletter_manager_factory'),
        'createNewsletterManager'
    ));
    
    $container->setDefinition('app.newsletter_manager', $newsletterManager);
    

Note

When using a factory to create services, the value chosen for the class option has no effect on the resulting service. The actual class name only depends on the object that is returned by the factory. However, the configured class name may be used by compiler passes and therefore should be set to a sensible value.

Passing Arguments to the Factory Method

If you need to pass arguments to the factory method, you can use the arguments options inside the service container. For example, suppose the createNewsletterManager() method in the previous example takes the templating service as an argument:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    services:
        # ...
    
        app.newsletter_manager:
            class:     AppBundle\Email\NewsletterManager
            factory:   ['@newsletter_manager_factory', createNewsletterManager]
            arguments: ['@templating']
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
            <!-- ... -->
    
            <service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
                <factory service="app.newsletter_manager_factory" method="createNewsletterManager"/>
                <argument type="service" id="templating"/>
            </service>
        </services>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    use Symfony\Component\DependencyInjection\Reference;
    use Symfony\Component\DependencyInjection\Definition;
    
    // ...
    $newsletterManager = new Definition('AppBundle\Email\NewsletterManager', array(
        new Reference('templating')
    ));
    $newsletterManager->setFactory(array(
        new Reference('app.newsletter_manager_factory'),
        'createNewsletterManager'
    ));
    $container->setDefinition('app.newsletter_manager', $newsletterManager);
    

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.