Dependency injection 尝试执行依赖项注入时,找不到自定义插件块的类
我正在尝试在Drupal中创建一个自定义插件块。当尝试实际访问我已注册的服务时,我继续遇到以下异常:Dependency injection 尝试执行依赖项注入时,找不到自定义插件块的类,dependency-injection,drupal,drupal-8,drupal-modules,Dependency Injection,Drupal,Drupal 8,Drupal Modules,我正在尝试在Drupal中创建一个自定义插件块。当尝试实际访问我已注册的服务时,我继续遇到以下异常: NOTICE: PHP message: Error: Class 'Drupal\MyNamespace\MyRegisteredService' not found in /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 #0 /var/www/html/web/
NOTICE: PHP message: Error: Class 'Drupal\MyNamespace\MyRegisteredService' not found in /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 #0 /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\Component\DependencyInjection\Container->createService(Array, 'foo....')
我已经正确地注册了服务,并且正确地设置了依赖注入(我相信),只是访问了不起作用的服务
我的文件结构当前看起来像:
- web
- modules
- custom
- foo
- foo.services.yml
- src
- MyService.php
- Plugin
- Block
- FooBlock.php
services:
foo.my_service:
class: Drupal\MyNamespace\MyRegisteredService
autowire: true
<?php
namespace Drupal\MyNamespace;
class MyService
{
static function say_hello()
{
return 'hello world';
}
}
foo.services.yml看起来像:
- web
- modules
- custom
- foo
- foo.services.yml
- src
- MyService.php
- Plugin
- Block
- FooBlock.php
services:
foo.my_service:
class: Drupal\MyNamespace\MyRegisteredService
autowire: true
<?php
namespace Drupal\MyNamespace;
class MyService
{
static function say_hello()
{
return 'hello world';
}
}
FooBlock.php看起来像(仅用于依赖项注入):
MyService.php看起来像:
- web
- modules
- custom
- foo
- foo.services.yml
- src
- MyService.php
- Plugin
- Block
- FooBlock.php
services:
foo.my_service:
class: Drupal\MyNamespace\MyRegisteredService
autowire: true
<?php
namespace Drupal\MyNamespace;
class MyService
{
static function say_hello()
{
return 'hello world';
}
}
我不确定您的服务或命名空间路径的实际名称,但我可以在占位符引用中看到一些不一致之处:
- 请将
Drupal\MyNamespace\MyRegisteredService
的所有引用更改为Drupal\foo\MyRegisteredService
,其中foo
是模块的名称。这应该是较低的情况
- 其次,
Drupal\MyNamespace\MyRegisteredService
引用一个类名MyRegisteredService
,而您给出的示例类名是MyService
。这些应该是相等的,所以要么选择一个,要么选择另一个
我的问题与名称空间的定义有关,类名是myside重命名东西时的错误。将Foo
更改为Foo
解决了问题。