Dependencies
Configure services, dependency injection, and tagging.
Complete services.yaml
# Resources/config/services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
# Namespace auto-registration
Plugins\MyPlugin\:
resource: '../../src/'
exclude:
- '../../src/Entity/'
- '../../src/Migrations/'
# Bootstrap (must be public)
Plugins\MyPlugin\Bootstrap:
public: true
# Controllers
Plugins\MyPlugin\Controller\:
resource: '../../src/Controller/'
tags: ['controller.service_arguments']
# CRUD Controllers (must be public + tagged)
Plugins\MyPlugin\Controller\Admin\:
resource: '../../src/Controller/Admin/'
tags: ['controller.service_arguments']
public: true
# Widgets (must be tagged)
Plugins\MyPlugin\Widget\:
resource: '../../src/Widget/'
tags: ['widget']
# Payment Providers (must be tagged + public)
Plugins\MyPlugin\Provider\:
resource: '../../src/Provider/'
tags: ['payment.provider']
public: true
# Event Subscribers (NO kernel.event_subscriber tag!)
Plugins\MyPlugin\EventSubscriber\:
resource: '../../src/EventSubscriber/'
public: true
# Cron Tasks (must be tagged)
Plugins\MyPlugin\CronTask\:
resource: '../../src/CronTask/'
tags: ['plugin.cron_task']
# Commands (auto-discovered by Symfony)
Plugins\MyPlugin\Command\:
resource: '../../src/Command/'
tags: ['console.command']
# Custom Services
Plugins\MyPlugin\Service\MyService:
arguments:
$apiKey: '%env(MY_PLUGIN_API_KEY)%'Tagging Requirements
Bootstrap
None
✅ Yes
Must be public
Controllers
controller.service_arguments
No
Standard controllers
CRUD Controllers
controller.service_arguments
✅ Yes
EasyAdmin controllers
Widgets
widget
No
Dashboard/page widgets
Payment Providers
payment.provider
✅ Yes
Payment integrations
Event Subscribers
None
✅ Yes
DO NOT tag with kernel.event_subscriber
Cron Tasks
plugin.cron_task
No
Scheduled tasks
Commands
console.command
No
CLI commands
Tabs
None
No
Registered via EventSubscriber
Dependency Injection
Constructor Injection
Method Injection
Service Aliases
Service Decoration
Environment Variables
Define in services.yaml
Set in .env
Use in PHP
Plugin Dependencies (composer.json)
External Packages
Installing Dependencies
Declaring in plugin.json
Plugin-to-Plugin Dependencies
PteroCA will ensure dependencies are installed and enabled before your plugin.
Lazy Services
For services that are expensive to instantiate:
Factory Services
Create services dynamically:
Service Locator
Access services dynamically:
Compiler Passes
For advanced service manipulation (rarely needed):
Debugging Services
Common Issues
Service Not Found
Problem: "Service not found"
Solutions:
Check namespace matches directory structure (PSR-4)
Ensure service is in
src/directoryClear cache:
php bin/console cache:clearVerify
services.yamlexcludes are correct
Circular Reference
Problem: "Circular reference detected"
Solution: Use lazy loading or refactor to break dependency cycle
Service Not Public
Problem: "Service is private"
Solution: Mark as public in services.yaml
Best Practices
Use autowiring for standard dependencies
Keep services focused - single responsibility
Inject interfaces not concrete classes when possible
Use constructor injection over method injection
Mark services as final unless designed for extension
Document constructor parameters for clarity
Related Guides
Bootstrap & Lifecycle - Bootstrap service
Controllers & Routes - Controller services
Payment Providers - Provider registration
Event System - Event subscriber registration
Last updated