admin管理员组文章数量:1122846
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:
.html
Multiple threads on stackoverflow regarding with naming convention.. identitation.. all tested, still not working. What's wrong here? Thanks for your help.
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.
Share Improve this question edited Nov 22, 2024 at 17:49 CitizenG1 asked Nov 22, 2024 at 17:35 CitizenG1CitizenG1 3179 silver badges21 bronze badges1 Answer
Reset to default 2You 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.
本文标签:
版权声明:本文标题:symfony - Cannot autowire service "": argument "$targetDirectory" of method "__cons 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301872a1931327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论