symfony - 找不到服务“fos_elastica.finder.app.user”
问题描述
Symfony 找不到 fos_elastica 的服务
Service "fos_elastica.finder.app.user" not found: even though it exists in the app's container, the container inside "App\Controller\DevController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "session" and "twig" services. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "DevController::getSubscribedServices()".
我的配置
# Read the documentation: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/Resources/doc/setup.md
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
# indexes:
# app: ~
indexes:
app:
client: default
types:
user:
properties:
username: ~
# mappings:
# email: ~
persistence:
# the driver can be orm, mongodb, phpcr or propel
# listener and finder are not supported by
# propel and should be removed
driver: orm
model: App\Entity\User
provider: ~
listener: ~
finder: ~
我的控制器:
/**
* @Route("/search")
*/
public function searchElastic(){
/** var array of App\Entity\User */
$finder = $this->container->get('fos_elastica.finder.app.user');
return new response('N/A');
}
命令php bin/console fos:elastica:populate
没有抛出任何错误,并且在 phpstorm 中没有突出显示(这意味着 phpstorm 找到了它)
请帮我。
解决方案
您应该像这样使用依赖注入:
在控制器类中添加use
语句use FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
,然后您的操作应如下所示:
/**
* @Route("/search")
*/
public function searchElastic(RepositoryManagerInterface $finder) {
$someResult = $finder->getRepository(User::class)->find(...);
return new response('N/A');
}
推荐阅读
- javascript - 如何在同一个网页中使用相同的 JavaScript 小部件 2 次?
- django - Postgres-alpine 无法连接到 Django 项目容器
- javascript - 如何将今天的时间添加到任何日期对象和 getISOString?
- django - django rest 框架中的 csrftoken - 通过 HTTPIE 发送
- python - pymysql.err.IntegrityError: (1048, "列 'dob' 不能为空")
- java - 如何在 Spring 5.3 及更高版本中使用 Stomp 和 SockJS 处理 CORS 起源?
- c++ - 返回对临时对象的引用
- android - Android Kotlin - 动画翻译X忽略距离并离开屏幕
- css - 响应式列表样式
- haskell - Haskell 理解类型