Skip to content

Using a Factory to Create Services

Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.1 (the current stable version).

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 by calling the static createNewsletterManager() method:

1
2
3
4
5
6
7
8
9
10
11
class NewsletterManagerStaticFactory
{
    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 NewsletterManagerStaticFactory::createNewsletterManager() factory method:

1
2
3
4
5
6
7
# app/config/services.yml

services:
    app.newsletter_manager:
        class:   AppBundle\Email\NewsletterManager
        # call the static method
        factory: ['AppBundle\Email\NewsletterManagerStaticFactory', createNewsletterManager]

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.

If your factory is not using a static function to configure and create your service, but a regular method, you can instantiate the factory itself as a service too. Later, in the "Using a Factory to Create Services" section, you learn how you can inject arguments in this method.

Configuration of the service container then looks like this:

1
2
3
4
5
6
7
8
9
10
# app/config/services.yml

services:
    app.newsletter_manager_factory:
        class: AppBundle\Email\NewsletterManagerFactory

    app.newsletter_manager:
        class:   AppBundle\Email\NewsletterManager
        # call a method on the specified factory service
        factory: 'app.newsletter_manager_factory:createNewsletterManager'

Note

The traditional configuration syntax in YAML files used an array to define the factory service and the method name:

1
2
3
4
5
6
7
# app/config/services.yml

app.newsletter_manager:
    # new syntax
    factory: 'app.newsletter_manager_factory:createNewsletterManager'
    # old syntax
    factory: ['@app.newsletter_manager_factory', createNewsletterManager]

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:

1
2
3
4
5
6
7
8
9
# app/config/services.yml

services:
    # ...

    app.newsletter_manager:
        class:     AppBundle\Email\NewsletterManager
        factory:   'newsletter_manager_factory:createNewsletterManager'
        arguments: ['@templating']
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version