T
T
tigra2020-07-17 10:19:23
PHP
tigra, 2020-07-17 10:19:23

How to enable autowiring using Symfony components?

This is what Kernel.php looks like

<?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 looks like this
<?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);
    }
}

I get an error
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.

How to connect autowiring so that it works in the controller? I assemble the project simply from the components, I did not take the skeleton. Routes work fine

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tigra, 2020-07-17
@tigroid3

I figured out that the service lacked a tag

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question