-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
[Cookbook] [CLI] Getting Services from the Service Container example does not work #890
Comments
Follow up: I believe a decent solution to this is as follows: <?php
protected function execute(InputInterface $input, OutputInterface $output)
{
// Retrieve the locale from the user input (e.g. 'en_US')
$locale = $input->getArgument('locale');
$translator = $this->getContainer()->get('translator');
$translator->setLocale($locale);
$name = $input->getArgument('name');
if ($name) {
$output->writeln($translator->trans('Hello %name%!', array('%name%' => $name)));
} else {
$output->writeln($translator->trans('Hello!'));
}
} I believe this goes hand in hand with the 2.1 BC break regarding the locale and how it is part of the request. In some respect the CLI command is a request, therefore the locale can come from the command line arguments (i.e. What do you think? Make a PR? |
Hi @jonathaningram ! Sorry for the slow response, but you've diagnosed the situation well. Unfortunately, I don't get the same error as you when I try it in 2.0. And I do like your second code-block (because it explicitly sets the locale, which is a great thing to show), but if there were an error, it would happen when retrieving the Do you remember more about this issue? Were you trying on 2.1 (the master branch) when you did this? |
Hello @weaverryan - sorry my slow response! I believe I was using I just checked my code, and you'll get this error at the following stack:
In the stack trace, we get to calling the <?php
/**
* {@inheritdoc}
*/
public function getLocale()
{
if (null === $this->locale && $this->container->has('request')) {
$this->locale = $this->container->get('request')->getLocale();
}
return $this->locale;
} Which tries to get Note: I am on some flavour of master, not the same as when I filed the original issue. So unless there is some new check (i.e. after the Hope that helps... |
Basically, the issue here is obviously that there's no request in the CLI. Usually, if you have a problem, you're trying to do something you shouldn't do in a command, since it makes sense that there is no CLI. But obviously, it does make sense to try to translate things. I think we should add a note about the issue of the missing request service and the types of issues it may cause. The solution will be different each time, but we can highlight that. For example, in the case of the Point is, if we add a note about it or a small section, then I think we're properly helping people. |
Oh, and in this case, the |
In the note, we can link to Moreover, I think a |
… (javiereguiluz) This PR was merged into the 2.3 branch. Discussion ---------- Fixed the section about getting services from a command | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | 2.3+ | Fixed tickets | #890 Hopefully this PR fixes one of the oldest errors of the Symfony documentation. Commits ------- 8993beb Linked the cookbook about DIC scopes 0f3c218 Fixed the section about getting services from a command
Fixed finally! :) |
I am troubleshooting the annoying
You cannot create a service ("request") of an inactive scope ("request").
CLI error, and I checked the docs to see if they successfully used the translator in a command. I found the following example, but it appears to fail with the same error too, so I propose either a) fix the example so that it works (thus helping me too ;) ) or b) changing the example to retrieve a different service that does not depend on therequest
.The doc URL is here
The text was updated successfully, but these errors were encountered: