我正在使用一些Symfony组件(而不是框架),并且试图在我定义为服务的控制器中注入依赖项,但是我遇到了致命的“参数过多错误”,因为没有注入依赖项。
我的组件是4.2版。
我的index.php(前端控制器)
<?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']
routes.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');
}
}
答案 0 :(得分:0)
如评论中所述,我必须使用ContainerControllerResolver而不是ControllerResolver,而且我还需要将服务ID更新为完全合格的域名。我还开始使用HttpKernel处理控制器逻辑。
index.php
require_once(__DIR__ . '/../vendor/autoload.php');
use Symfony\Component\Config\FileLocator;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Site\Site;
require_once('../config.php');
require_once('containerBuilder.php');
$fileLocator = new FileLocator([__DIR__]);
$loader = new YamlFileLoader($fileLocator);
$routes = $loader->load('routes.yml');
$request = Request::createFromGlobals();
$requestStack = new RequestStack();
$context = new RequestContext();
$context->fromRequest($request);
$matcher = new UrlMatcher($routes, $context);
$controllerResolver = new HttpKernel\Controller\ContainerControllerResolver($container);
$argumentResolver = new HttpKernel\Controller\ArgumentResolver();
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new HttpKernel\EventListener\RouterListener($matcher, $requestStack));
$site = new Site($dispatcher, $controllerResolver, $requestStack, $argumentResolver);
$response = $site->handle($request);
$response->send();
services.yml
services:
\Site\ApiConnection\RezdyApiConnection:
class: \Site\ApiConnection\RezdyApiConnection
arguments: []
\Site\Controller\ApiController:
class: \Site\Controller\ApiController
arguments: ['@\Site\ApiConnection\RezdyApiConnection']