Аргумент автоподключения Symfony 4 не работает

Я пытаюсь подключить UrlGeneratorInterface в DTO, чтобы использовать метод generate в моем DTO,

У меня есть это в моем DTO:

namespace App\DTO;

use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use App\Entity\News;
use App\Application\Sonata\MediaBundle\Entity\Media;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;


/**
 * @ExclusionPolicy("all")
 *
 */
class NewsDTO
{

    /**
     * @var integer|null
     * @Serializer\Groups({"news"})
     * @Expose
     */
    public $id;

    /**
     * @var string|null
     * @Serializer\Groups({"news"})
     * @Expose
     */
    public $title;


    /**
     * @var string|null
     * @Serializer\Groups({"news"})
     * @Expose
     */
    public $text;

    /**
     * @var string|null
     * @Serializer\Groups({"news"})
     * @Expose
     */
    public $cover_image;


    /**
     * @var string|null
     * @Serializer\Groups({"news"})
     * @Expose
     */
    public $description;

    /**
     * @var string|null
     * @Serializer\Groups({"news"})
     * @Expose
     */
    public $web_url;

    /**
     * @var string|null
     * @Serializer\Groups({"news"})
     * @Expose
     */
    public $link;

    /**
     * @var datetime|null
     * @Serializer\Groups({"news"})
     * @Expose
     */
    private $news_date;

    private $router;

    public function __construct(UrlGeneratorInterface $urlGenerator){
        $this->router = $router;
    }

и мой service.yaml:

parameters:
    locale: 'fr'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Application,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    dto.news :
        class: App\DTO\NewsDTO
        arguments: 
            $urlGenerator: ['@router.default']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
    admin.videos:
        class: App\Admin\VideoAdmin
        arguments: [~, App\Entity\Video, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Video }
    admin.news:
        class: App\Admin\NewsAdmin
        arguments: [~, App\Entity\News, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Actualités }
    admin.prerolls:
        class: App\Admin\PrerollAdmin
        arguments: [~, App\Entity\Preroll, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Preroll }

Но возвращенная ошибка:

 "message": "Too few arguments to function App\\DTO\\NewsDTO::__construct(), 0 passed in /data/www/api-dev.chartres.live/www/src/Representation/Actus.php on line 35 and exactly 1 expected"

1 ответ

Скорее всего, вы неправильно звоните в службу.

В /data/www/api-dev.chartres.live/www/src/Representation/Actus.php вы, вероятно, делаете что-то вроде:

$newsDto = new NewsDTO();

Но вместо этого вам нужно использовать служебный инжектор:

// Assuming this is a controller or has access to the `get()` service loopup
$newsDto = $this->get('dto.news');
Другие вопросы по тегам