我正在使用语言环境构建Symfony 4应用程序。 我的语言环境绑定在services.yaml中:
parameters:
locale: 'en'
app_locales: en|fr|de|es|pt|
services:
_defaults:
autowire: true
autoconfigure: true
bind:
$locale: "@=service('request_stack').getCurrentRequest().getLocale()"
$listLocales: '%app_locales%'
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Repository\:
esource: '../src/Repository'
此外,语言环境已注入并且可以在我的存储库中使用(用于弹性搜索请求)。
我需要添加一个使用我的存储库之一来检索一些数据的命令,但出现此错误:
In getSomeRepositoryService.php line 12:
Call to a member function getLocale() on null
var / cache / dev / ContainerJyByO4D / getSomeRepositoryService.php:
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the private 'App\Repository\SomeRepository' shared autowired service.
include_once $this->targetDirs[3].'/src/Repository/AbstractRepository.php';
include_once $this->targetDirs[3].'/src/Repository/SomeRepository.php';
return $this->privates['App\Repository\SomeRepository'] = new \App\Repository\SomeRepository(($this->privates['Elastica\Client'] ?? $this->load('getClientService.php')), ($this->services['request_stack'] ?? ($this->services['request_stack'] = new \Symfony\Component\HttpFoundation\RequestStack()))->getCurrentRequest()->getLocale());
将语言环境注入AbstractRepository构造函数中。
这是我的命令(简化版):
classFooCommand extends Command
{
protected $someRepository;
public function __construct(SomeRepository $someRepository)
{
$this->someRepository = $someRepository;
parent::_construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$demoData = $this->someRepository->getFoo();
var_dump($demoData);
}
}
如何在我的命令中正确插入语言环境?