Skip to content

Factory

Each entity should have a factory that helps to generate a new entity. A factory must implements App\Core\Factory\FactoryInterface.

Generation

A factory is basically a service which contain at lease a method named create.

The generation is performed in CLI. These information are required:

  • The name of the futur factory (eg: MyEntityFactory)
  • The namespace of the entity (eg: MyEntity)

Simply run php bin/console make:factory.

Usage

src/Controller/FooController.php
namespace App\Controller;

use App\Factory\MyEntityFactory;
use Symfony\Component\HttpFoundation\Response;

class FooController
{
    public function foo(MyEntityFactory $factory): Response
    {
        $entity = $factory->create();

        // ...
    }
}