Я использую некоторые компоненты Symfony (не каркас) и пытаюсь внедрить зависимость в контроллере, который я определил как службу, однако я получаю фатальную ошибку «слишком мало аргументов», так какзависимости не вводятся.
Мои компоненты версии 4.2.
Мой index.php (Front Controller)
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
require_once('../config.php');
require_once('containerBuilder.php');
$fileLocator = new FileLocator([__DIR__]);
$loader = new YamlFileLoader($fileLocator);
$routes = $loader->load('routes.yml');
$request = Request::createFromGlobals();
$context = new RequestContext();
$context->fromRequest($request);
$matcher = new UrlMatcher($routes, $context);
$controllerResolver = new HttpKernel\Controller\ControllerResolver();
$argumentResolver = new HttpKernel\Controller\ArgumentResolver();
try {
$request->attributes->add($matcher->match($request->getPathInfo()));
$controller = $controllerResolver->getController($request);
$arguments = $argumentResolver->getArguments($request, $controller);
$response = call_user_func_array($controller, $arguments);
} catch (ResourceNotFoundException $e) {
$response = new Response('Not Found', 404);
} catch (Exception $e) {
$response = new Response('An error occurred', 500);
}
$response->send();
services.yml
services:
api_connection.rezdy:
class: \Site\ApiConnection\RezdyApiConnection
arguments: []
controller.api_controller:
class: \Site\Controller\ApiController
arguments: ['@api_connection.rezdy']
rout.yml
products:
path: /products
defaults: { _controller: '\Site\Controller\ApiController::getProducts' }
product:
path: /products/{id}
defaults: { _controller: '\Site\Controller\ApiController::index' }
ApiController.php
<?php
namespace Site\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Yaml\Yaml;
use Site\ApiConnection\ApiConnectionInterface;
class ApiController {
private $connection;
public function __construct(ApiConnectionInterface $connection) {
$this->connection = $connection;
}
public function getProducts() {
$details = Yaml::parseFile(WEB_ROOT . '/api_connection_details.yml');
return new JsonResponse('This is dog');
}
}