Answer the question
In order to leave comments, you need to log in
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');
}
}
<?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}');
};
<?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.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question