在我的Laravel 6.9.0应用程序中,我有一些代码需要在许多地方运行,因此我正在努力将其创建为服务层。
我已经创建了文件/app/Actions/Music/GetRecentArtists.php
,并将其添加到我的作曲家自动加载配置中:
<?php
namespace App\Actions\Music;
use \Barryvanveen\Lastfm\Lastfm;
class GetRecentArtists {
public function get(Lastfm $lastfm)
{
return true;
}
}
但是当我使用Tinker在Tinker中运行它时:
(new App\Actions\Music\GetRecentArtists())->get()
我收到以下错误:
TypeError: Too few arguments to function App/Actions/Music/GetRecentArtists::get(), 0 passed in Psy Shell code on line 1 and exactly 1 expected
我以为依赖项注入将注入Lastfm
实例。当我删除函数参数时,它运行良好。
如果使用resolve
方法,Laravel服务容器应为您注入依赖项:
resolve