英文:
Laravel Dependency Injection not working with new Service Provider
问题
我目前正在设置一个新服务的依赖注入时遇到了一些问题。
我试图让 PartyRingService
自动注入到构造函数中,而不需要手动传递它。
我在 PartyRingServiceProvider
中尝试了以下内容:
class PartyRingServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton('App\Services\PartyRingService', function ($app) {
return new PartyRingService();
});
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
//
}
}
我已经在 config/app.php
中的服务提供者数组中添加了我的新服务提供者:
/*
* Application Service Providers...
*/
...
App\Providers\PartyRingServiceProvider::class,
这是我尝试注入服务的构造函数:
这是我尝试在构造函数中使用我的新服务的类:
class Party
{
private PartyRingService $partyRingService;
public function __construct(PartyRingService $partyRingService)
{
$this->partyRingService = $partyRingService;
}
...
}
然而,当我尝试使用 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
:
class PartyRingServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton('App\Services\PartyRingService', function ($app) {
return new PartyRingService();
});
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
//
}
}
I have added to config/app.php
my new service provider to the providers array:
/*
* Application Service Providers...
*/
......
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:
class Party
{
private PartyRingService $partyRingService;
public function __construct(PartyRingService $partyRingService)
{
$this->partyRingService = $partyRingService;
}
...
}
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
你必须在你的控制器中使用这个:
$party = resolve(Party::class);
这样对你会有帮助。
英文:
You have to use this in your controller:
$party = resolve(Party::class);
And that will work for you
答案2
得分: 0
我不是完全确定,但在解决Party类时,应该添加PartyRingService。
public function register(): void
{
$this->app->singleton('App\Party', function ($app) {
return Party($app->make(PartyRingService::class));
});
}
英文:
I am not totally sure, but shouldn't you add PartyRingService when resolving Party class
{
$this->app->singleton('App\Party', function ($app) {
return Party($app->make(PartyRingService::class));
});
}
答案3
得分: -1
确保您已在控制器文件的顶部导入了Party类和PartyRingService类:
use App\Party;
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:
use App\Party;
use App\Services\PartyRingService;
This assumes that the Party class and the PartyRingService class are present in the correct namespace.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论