diff --git a/best_practices.rst b/best_practices.rst index 4928c79a6f9..d6eb4786efe 100644 --- a/best_practices.rst +++ b/best_practices.rst @@ -244,7 +244,7 @@ Use Dependency Injection to Get Services If you extend the base ``AbstractController``, you can only access to the most common services (e.g ``twig``, ``router``, ``doctrine``, etc.), directly from the -container via ``$this->container->get()`` or ``$this->get()``. +container via ``$this->container->get()``. Instead, you must use dependency injection to fetch services by :ref:`type-hinting action method arguments ` or constructor arguments. diff --git a/forms.rst b/forms.rst index 3814caa0646..08499f0239d 100644 --- a/forms.rst +++ b/forms.rst @@ -857,14 +857,15 @@ method:: use App\Form\TaskType; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + use Symfony\Component\Form\FormFactoryInterface; // ... class TaskController extends AbstractController { - public function new(): Response + public function new(FormFactoryInterface $formFactory): Response { $task = ...; - $form = $this->get('form.factory')->createNamed('my_name', TaskType::class, $task); + $form = $formFactory->createNamed('my_name', TaskType::class, $task); // ... }