diff --git a/cookbook/console/console_command.rst b/cookbook/console/console_command.rst index 7e23a4762a4..64d8c4a8d90 100644 --- a/cookbook/console/console_command.rst +++ b/cookbook/console/console_command.rst @@ -101,69 +101,6 @@ service container. In other words, you have access to any configured service:: // ... } -However, due to the :doc:`container scopes ` this -code doesn't work for some services. For instance, if you try to get the ``request`` -service or any other service related to it, you'll get the following error: - -.. code-block:: text - - You cannot create a service ("request") of an inactive scope ("request"). - -Consider the following example that uses the ``translator`` service to -translate some contents using a console command:: - - protected function execute(InputInterface $input, OutputInterface $output) - { - $name = $input->getArgument('name'); - $translator = $this->getContainer()->get('translator'); - if ($name) { - $output->writeln( - $translator->trans('Hello %name%!', array('%name%' => $name)) - ); - } else { - $output->writeln($translator->trans('Hello!')); - } - } - -If you dig into the Translator component classes, you'll see that the ``request`` -service is required to get the locale into which the contents are translated:: - - // vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php - public function getLocale() - { - if (null === $this->locale && $this->container->isScopeActive('request') - && $this->container->has('request')) { - $this->locale = $this->container->get('request')->getLocale(); - } - - return $this->locale; - } - -Therefore, when using the ``translator`` service inside a command, you'll get the -previous *"You cannot create a service of an inactive scope"* error message. -The solution in this case is as easy as setting the locale value explicitly -before translating contents:: - - protected function execute(InputInterface $input, OutputInterface $output) - { - $name = $input->getArgument('name'); - $locale = $input->getArgument('locale'); - - $translator = $this->getContainer()->get('translator'); - $translator->setLocale($locale); - - if ($name) { - $output->writeln( - $translator->trans('Hello %name%!', array('%name%' => $name)) - ); - } else { - $output->writeln($translator->trans('Hello!')); - } - } - -However, for other services the solution might be more complex. For more details, -see :doc:`/cookbook/service_container/scopes`. - Invoking other Commands ----------------------- diff --git a/cookbook/templating/twig_extension.rst b/cookbook/templating/twig_extension.rst index 679da017618..5b56c10ea08 100644 --- a/cookbook/templating/twig_extension.rst +++ b/cookbook/templating/twig_extension.rst @@ -99,16 +99,6 @@ Now you must let the Service Container know about your newly created Twig Extens ->setPublic(false) ->addTag('twig.extension'); -.. note:: - - Keep in mind that Twig Extensions are not lazily loaded. This means that - there's a higher chance that you'll get a - :class:`Symfony\\Component\\DependencyInjection\\Exception\\ServiceCircularReferenceException` - or a - :class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException` - if any services (or your Twig Extension in this case) are dependent on - the request service. For more information take a look at :doc:`/cookbook/service_container/scopes`. - Using the custom Extension --------------------------