From 179039ba9e48fcc8df47a5ef158d2bf531172b4d Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 28 Nov 2015 17:29:03 +0100 Subject: [PATCH 1/2] Remove information about request scpoe Services no longer depend on the request service, but instead on request_stack so this is not an issue anymore as of Symfony 2.4. --- cookbook/console/console_command.rst | 63 ---------------------------- 1 file changed, 63 deletions(-) diff --git a/cookbook/console/console_command.rst b/cookbook/console/console_command.rst index 184c51e541d..7b4940623f0 100644 --- a/cookbook/console/console_command.rst +++ b/cookbook/console/console_command.rst @@ -96,69 +96,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 ----------------------- From eeb579c939694738544a89a0a1bc3da71c2925a2 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 28 Nov 2015 17:35:33 +0100 Subject: [PATCH 2/2] Remove note about request service, which is not used anymore --- cookbook/templating/twig_extension.rst | 10 ---------- 1 file changed, 10 deletions(-) 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 --------------------------