If all your services are ContainerAware , I suggest creating a BaseService class that will contain all the common code with your other services.
1) Create a class Base\BaseService.php :
<?php namespace Fuz\GenyBundle\Base; use Symfony\Component\DependencyInjection\ContainerAware; abstract class BaseService extends ContainerAware { }
2) Register this service as abstract in your services.yml
parameters: // ... geny.base.class: Fuz\GenyBundle\Base\BaseService services: // ... geny.base: class: %geny.base.class% abstract: true calls: - [setContainer, [@service_container]]
3) Now in your other services, your BaseService class is BaseService instead of ContainerAware :
<?php namespace Fuz\GenyBundle\Services; use Fuz\GenyBundle\Base\BaseService; class Loader extends BaseService {
4) Finally, you can use the parent option in the services declaration.
geny.loader: class: %geny.loader.class% parent: geny.base
I prefer this way for several reasons:
- there is consistency between code and configuration
- this avoids duplication of too many configurations for each service.
- You have a base class for each service, very useful for general code.
Alain Tiemblo Sep 06 '15 at 16:02 2015-09-06 16:02
source share