Php 从其他服务调用具有存储库模式的Laravel调用服务
我在我的Laravel项目中使用 从其他服务调用服务的好模式是什么 例如,服务将如下所示:Php 从其他服务调用具有存储库模式的Laravel调用服务,php,laravel,Php,Laravel,我在我的Laravel项目中使用 从其他服务调用服务的好模式是什么 例如,服务将如下所示: class GetAllUsersService { private $userRepository; public function __construct(UserRepository $repository) { $this->userRepository = $repository; } public function execu
class GetAllUsersService
{
private $userRepository;
public function __construct(UserRepository $repository)
{
$this->userRepository = $repository;
}
public function execute()
{
return $this->userRepository->getAll();
}
}
class AnyClass
{
public function executeUserService()
{
$repository = new UserEloquentRepository();
$service = new GetAllUsersService($repository);
return $service->execute();
}
}
class AnyClass
{
public function index()
{
$get_all_users_service = app(GetAllUsersService::class);
$fetchAllUsers = $get_all_users_service->fetchAll();
}
}
现在,如果我想从应用程序的其他部分执行此服务,我将执行以下操作:
class GetAllUsersService
{
private $userRepository;
public function __construct(UserRepository $repository)
{
$this->userRepository = $repository;
}
public function execute()
{
return $this->userRepository->getAll();
}
}
class AnyClass
{
public function executeUserService()
{
$repository = new UserEloquentRepository();
$service = new GetAllUsersService($repository);
return $service->execute();
}
}
class AnyClass
{
public function index()
{
$get_all_users_service = app(GetAllUsersService::class);
$fetchAllUsers = $get_all_users_service->fetchAll();
}
}
这是正确的方法吗?还有别的办法吗?也许某个UI层应该介于两者之间?我认为有三种方法可以做到这一点: 1) 使用方法_construct() 2) 使用指定的服务,就像使用每个函数所需的参数一样:
class AnyClass
{
public function index(GetAllUsersService $get_all_users_service)
{
$fetchAllUsers = $get_all_users_service->fetchAll();
}
}
3) 使用Laravel helper的方法app(),如下所示:
class GetAllUsersService
{
private $userRepository;
public function __construct(UserRepository $repository)
{
$this->userRepository = $repository;
}
public function execute()
{
return $this->userRepository->getAll();
}
}
class AnyClass
{
public function executeUserService()
{
$repository = new UserEloquentRepository();
$service = new GetAllUsersService($repository);
return $service->execute();
}
}
class AnyClass
{
public function index()
{
$get_all_users_service = app(GetAllUsersService::class);
$fetchAllUsers = $get_all_users_service->fetchAll();
}
}
我认为有三种方法可以做到: 1) 使用方法_construct() 2) 使用指定的服务,就像使用每个函数所需的参数一样:
class AnyClass
{
public function index(GetAllUsersService $get_all_users_service)
{
$fetchAllUsers = $get_all_users_service->fetchAll();
}
}
3) 使用Laravel helper的方法app(),如下所示:
class GetAllUsersService
{
private $userRepository;
public function __construct(UserRepository $repository)
{
$this->userRepository = $repository;
}
public function execute()
{
return $this->userRepository->getAll();
}
}
class AnyClass
{
public function executeUserService()
{
$repository = new UserEloquentRepository();
$service = new GetAllUsersService($repository);
return $service->execute();
}
}
class AnyClass
{
public function index()
{
$get_all_users_service = app(GetAllUsersService::class);
$fetchAllUsers = $get_all_users_service->fetchAll();
}
}
第一种方法不适合我的情况,我必须将存储库传递给
AnyClass
,第二种方法还将说明您必须将服务传递给index
函数。但是第三个看起来很棒!很好,这对您很有帮助。第一种方法不适合我的情况,我必须将存储库传递给AnyClass
,第二种方法还将说明您必须将服务传递给index
函数。但是第三个看起来很棒!很好,这对你很有帮助。