skip to Main Content

I have a Symfony’s services.yaml which defines a "basic" container configuration and import di.php files to configure some specific services.

imports:
  - { resource: '../src/**/di.php' }

services:
  _defaults:
    autowire: true
    autoconfigure: true

App:
  resource: '../src/'
    exclude:
      - '../src/**/di.php'
      - '../src/**/Entity/'
      - '../src/**/Model/'
      - '../src/Authorization/AuthorizationProcessor/'
      - '../src/Kernel.php'

For instance, here, I explicitly exclude src/Authorization/Processor folder because it has a di.php file that holds service definitions for this specific namespace.

This approach works.

But when I replace the services.yaml with services.php

<?php
return static function (ContainerConfigurator $di): void {
    $di->import('../src/**/di.php');

    $services = $di->services()
                   ->defaults()
                   ->autowire()
                   ->autoconfigure();

    $services->load('App\', '../src/')
             ->exclude([
                 '../src/**/di.php',
                 '../src/**/Entity/',
                 '../src/**/Model/',
                 '../src/Authorization/AuthorizationProcessor/',
                 '../src/Kernel.php',
             ]);

To me, it looks the same but with services.php I get an error

The file "../src" does not exist (in:
"/var/www/app/src/Authorization/AuthorizationProcessor") in
/var/www/app/config/services.php (which is being imported from
"/var/www/app/src/Kernel.php").

2

Answers


  1. Chosen as BEST ANSWER

    I've figured it out. It has nothing to do with path. What is important - the order.

    When it is YAML than you could have first imports, then services section. But when it is PHP - the import has to be the last instruction.

    As soon as I've moved the import after the $services->load it starts working as expected.


  2. I’d try replacing all the paths with this format:

    $di->import(__DIR__.'/../src/**/di.php');
    

    Note the added / in the beginning of the string.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search