Laravel service container can be associated with a class And all its ancestors - as in Symfony

I have a base class:

class BaseAPI { public function __construct(Client $client, CacheInterface $cache, $apiBaseUri) { $this->client = $client; $this->apiBaseUri = $apiBaseUri; $this->cache = $cache; } } 

And then in the provider:

 class APIServiceProvider extends ServiceProvider { public function register() { $this->app ->when(BaseAPI::class) ->needs('$apiBaseUri') ->give(env('DEFAULT_API_URI','')); } } 

It works. But when I do the ancestor:

 class GetLocations extends BaseAPI { protected $apiMethodName = 'locations'; protected $type = 'get'; } 

I get an error. Of course, I can manually write code for each of the ancestors, but there are many of them, so the question is: is there any inheritance mechanism for binding? Something like this: https://symfony.com/doc/current/service_container/parent_services.html

+5
source share
1 answer

Have you tried using the interface for this?

 class BaseAPI implements APIInterface { } class APIServiceProvider extends ServiceProvider { public function register() { $this->app ->when(APIInterface::class) ->needs('$apiBaseUri') ->give(env('DEFAULT_API_URI','')); } } 
0
source

Source: https://habr.com/ru/post/1261505/


All Articles