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

Component
Tag
Public?
Notes

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:

  1. Check namespace matches directory structure (PSR-4)

  2. Ensure service is in src/ directory

  3. Clear cache: php bin/console cache:clear

  4. Verify services.yaml excludes 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

  1. Use autowiring for standard dependencies

  2. Keep services focused - single responsibility

  3. Inject interfaces not concrete classes when possible

  4. Use constructor injection over method injection

  5. Mark services as final unless designed for extension

  6. Document constructor parameters for clarity

Last updated