tigroid3
@tigroid3
PHP, YII2, SQL, Postgres, Docker, SPHINX, GIT

Как используя компоненты Symfony подключить autowiring?

Вот так выглядит Kernel.php
<?php

namespace App;

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    public function getProjectDir(): string
    {
        return \dirname(__DIR__);
    }

    public function registerBundles(): array
    {
        return [
            new FrameworkBundle(),
        ];
    }

    protected function configureContainer(ContainerConfigurator $container): void
    {
        $confifDir = $this->getProjectDir() . '/config';

        $container->import($confifDir . '/services.php', 'glob');
    }

    protected function configureRoutes(RoutingConfigurator $routes): void
    {
        $confifDir = $this->getProjectDir() . '/config';

        $routes->import($confifDir . '/routes.php');
    }
}

config/services.php выглядит так
<?php

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $configurator) {
    $services = $configurator->services()
        ->defaults()
        ->autowire()
        ->autoconfigure();

    $services->load('App\\', '../src/*')
        ->exclude('../src/{Entities,Kernel2.php}');
};

controller
<?php

namespace App\Controllers;

use App\Repositories\TestRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;

/**
 * Class OrderController
 * @package App\Controllers
 */
class OrderController extends ControllerResolver
{
    /**
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @param \App\Repositories\TestRepository $testRepository
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request, TestRepository $testRepository)
    {
        /** @var string $t */
        $t = $testRepository->build();

        return new Response($t);
    }
}

Получаю ошибку
Controller "App\Controllers\OrderController::index()" requires that you provide a value for the "$testRepository" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

Как подключить autowiring, чтобы в контроллере работало? проект собираю просто из компонентов, не брал skeleton. Роуты работают норм
  • Вопрос задан
  • 162 просмотра
Решения вопроса 1
tigroid3
@tigroid3 Автор вопроса
PHP, YII2, SQL, Postgres, Docker, SPHINX, GIT
Разобрался, в сервисе не хватало tag
$services->load('App\\', '../src/*')
        ->exclude('../src/{Entities,Kernel.php}')
        ->tag('controller.service_arguments');
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
19 апр. 2024, в 13:31
10000 руб./за проект
19 апр. 2024, в 13:12
35000 руб./за проект
19 апр. 2024, в 13:06
6000 руб./за проект