Laravel 依赖注入在新的服务提供者中不起作用。

huangapple go评论80阅读模式
英文:

Laravel Dependency Injection not working with new Service Provider

问题

我目前正在设置一个新服务的依赖注入时遇到了一些问题。

我试图让 PartyRingService 自动注入到构造函数中,而不需要手动传递它。

我在 PartyRingServiceProvider 中尝试了以下内容:

  1. class PartyRingServiceProvider extends ServiceProvider
  2. {
  3. /**
  4. * Register any application services.
  5. */
  6. public function register(): void
  7. {
  8. $this->app->singleton('App\Services\PartyRingService', function ($app) {
  9. return new PartyRingService();
  10. });
  11. }
  12. /**
  13. * Bootstrap any application services.
  14. */
  15. public function boot(): void
  16. {
  17. //
  18. }
  19. }

我已经在 config/app.php 中的服务提供者数组中添加了我的新服务提供者:

  1. /*
  2. * Application Service Providers...
  3. */
  4. ...
  5. App\Providers\PartyRingServiceProvider::class,

这是我尝试注入服务的构造函数:

这是我尝试在构造函数中使用我的新服务的类:

  1. class Party
  2. {
  3. private PartyRingService $partyRingService;
  4. public function __construct(PartyRingService $partyRingService)
  5. {
  6. $this->partyRingService = $partyRingService;
  7. }
  8. ...
  9. }

然而,当我尝试使用 PartyService 时,我得到了以下错误:

ArgumentCountError PHP 8.2.7
10.13.5 Too few arguments to function App\Party::__construct(), 0 passed in
/var/www/html/app/Http/Controllers/PartyController.php on line 28
and exactly 1 expected

这是 PartyController 的第 28 行代码:

$party = new Party();

我感觉我可能只是漏掉了一个步骤,如果有人可以帮助我识别出来,那将非常好,谢谢。

英文:

I am currently having some issues setting up some dependency injection for a new service I have made.

I am trying to get PartyRingService to inject into a constructor without me passing it in.

I have tried the following in my PartyRingServiceProvider:

  1. class PartyRingServiceProvider extends ServiceProvider
  2. {
  3. /**
  4. * Register any application services.
  5. */
  6. public function register(): void
  7. {
  8. $this->app->singleton('App\Services\PartyRingService', function ($app) {
  9. return new PartyRingService();
  10. });
  11. }
  12. /**
  13. * Bootstrap any application services.
  14. */
  15. public function boot(): void
  16. {
  17. //
  18. }
  19. }

I have added to config/app.php my new service provider to the providers array:

  1. /*
  2. * Application Service Providers...
  3. */
  4. ......
  5. App\Providers\PartyRingServiceProvider::class,

And this is the constructor where I am trying to inject my service:

This is the class where I am trying to use my new service in the constructor:

  1. class Party
  2. {
  3. private PartyRingService $partyRingService;
  4. public function __construct(PartyRingService $partyRingService)
  5. {
  6. $this->partyRingService = $partyRingService;
  7. }
  8. ...
  9. }

However, I am getting the following error when I am trying to use PartyService:

> ArgumentCountError PHP 8.2.7
> 10.13.5 Too few arguments to function App\Party::__construct(), 0 passed in
> /var/www/html/app/Http/Controllers/PartyController.php on line 28
> and exactly 1 expected

This is what line 28 of PartyController looks like:

$party = new Party();

I feel like I am just missing a step here, so if someone could help me identify it, that would be great, thank you.

答案1

得分: 0

你必须在你的控制器中使用这个:

  1. $party = resolve(Party::class);

这样对你会有帮助。

英文:

You have to use this in your controller:

  1. $party = resolve(Party::class);

And that will work for you

答案2

得分: 0

我不是完全确定,但在解决Party类时,应该添加PartyRingService。

  1. public function register(): void
  2. {
  3. $this->app->singleton('App\Party', function ($app) {
  4. return Party($app->make(PartyRingService::class));
  5. });
  6. }
英文:

I am not totally sure, but shouldn't you add PartyRingService when resolving Party class

  1. {
  2. $this->app->singleton('App\Party', function ($app) {
  3. return Party($app->make(PartyRingService::class));
  4. });
  5. }

答案3

得分: -1

确保您已在控制器文件的顶部导入了Party类和PartyRingService类:

  1. use App\Party;
  2. use App\Services\PartyRingService;

这假设Party类和PartyRingService类在正确的命名空间中存在。

英文:

Make sure that you have imported the Party class and the PartyRingService class at the top of your controller file:

  1. use App\Party;
  2. use App\Services\PartyRingService;

This assumes that the Party class and the PartyRingService class are present in the correct namespace.

huangapple
  • 本文由 发表于 2023年6月29日 00:03:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76574925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定