Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove information about request service deps of core services #5923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 0 additions & 63 deletions cookbook/console/console_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,69 +96,6 @@ service container. In other words, you have access to any configured service::
// ...
}

However, due to the :doc:`container scopes </cookbook/service_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
-----------------------

Expand Down
10 changes: 0 additions & 10 deletions cookbook/templating/twig_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------

Expand Down