symfony

Cannot autowire service "": argument "$targetDirectory" of method "__construct()" is type-hinted "string", you should configure its value explicitly


I'm trying to configure a service. I have been this before without any kind of problem. But now seems impossible with Symfony 7.1

I already searched for some hours here and doesn't works anything, i felt stupid.

This is my config/services.yaml:

parameters:
   frontend_base_url: '%env(FRONTEND_BASE_URL)%'

services:
  _defaults:
      autowire: true
      autoconfigure: true

  App\EventSubscriber\AuthenticationSuccessSubscriber:
      tags:
          - { name: kernel.event_subscriber }
  App\Exception\UserNotActivatedException:
      tags:
          - { name: kernel.event_listener, event: kernel.exception }
  App\Service\SnapshotService:
      arguments:
          $targetDirectory: '%kernel.project_dir%/public/uploads/snapshots'
  App\:
      resource: '../src/'
      exclude:
          - '../src/DependencyInjection/'
          - '../src/Entity/'
          - '../src/Kernel.php'

This is my service located at ./src/Service/SnapshotService.php

<?php

namespace App\Service;

use mikehaertl\wkhtmlto\Image;

class SnapshotService
{

  private string $targetDirectory;

  public function __construct(string $targetDirectory){
      $this->targetDirectory = $targetDirectory;
  }

  public function createSnapshot(string $url, string $filename)
  {
      $outputPath = $this->getTargetDirectory() . '/' . $filename;

      $image = new Image([
          'url' => $url,
          'quality' => 90,
          'format' => 'jpg',
          'width' => 390,
      ]);

      if (!$image->saveAs($outputPath)) {
          throw new \Exception('Error generating image: ' . $image->getError());
      }

      return $outputPath;
  }

  public function getTargetDirectory(): string
  {
      return $this->targetDirectory;
  }
}

The error received is this:

[Web Server ] [22-Nov-2024 17:23:40 UTC] 2024-11-22T17:23:40+00:00 [critical] Uncaught Exception: Cannot autowire service "App\Service\SnapshotService": argument "$targetDirectory" of method "__construct()" is type-hinted "string", you should configure its value explicitly

I used this same code on Symfony 4 and works. Verified documentation:

https://symfony.com/doc/current/controller/upload_file.html

Multiple threads on stackoverflow regarding with naming convention.. identitation.. all tested, still not working. What's wrong here? Thanks for your help.


Solution

  • You need to place the global configuration (App:) at the very top, before declaring any other services:

    services:
    _defaults:
        autowire: true
        autoconfigure: true
    
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
    
    App\Service\SnapshotService:
        arguments:
            $targetDirectory: '%kernel.project_dir%/public/uploads/snapshots'
    

    In this version, the order has somehow become important. Previously, it truly didn’t matter.