Symfony 7: Fundamentals
Service
Service container
- contains services but also parameters;
# Show services
symfony console debug:container
Autowiring
- can autowire: service, parameter, doctrine entity, symfony request class;
- available in:
- controller: constructor (recommended), and methods (avoid);
- service constructor;
- if multiple services implement the same interface, one option is to use Named autowiring
# Show all the services that are autowireable
php bin/console debug:autowiring
# Search for service
php bin/console debug:autowiring cache
Autowire non-autowireable service
Bundle
- provide stuff, including services
- needs activating (recipe does that automatically;
- register bundles in application:
config/bundles.php
;
Symfony\Bundle\FrameworkBundle\FrameworkBundle
registers services when installing Symfony components;
config/packages/
: configure specific bundle;
# Get info about one specific bundle
symfony console debug:container http
# Check if it can be autowired
symfony console debug:autowiring http
Parameters
- they are in the service container
- available in controller:
getParameter()
- can be available as service parameters but need autowire configuration: Non-Autowireable Arguments
- approach: create a local factory/service and send the parameters from the controller to the service method;
- stuff that can change on each computer: .env
- stuff that does not change on each computer, but can be different between environments:
config/services_dev.yaml
, config/services_prod.yaml
- can read stuff from
.env
: Environment Variables
# See list
bin/console debug:container --parameters