0

我在创建您自己的 PHP 框架之后的项目中使用 Symfony DIC、路由和 ControllerResolver

我不明白如何在不使用配置组件或在控制器中设置容器的情况下将服务正确传输到控制器的构造函数?路由:

$routes->add('mycontroller', new Routing\Route('/', ['_controller' => 'controller.mycontroller::indexAction']));

控制器:

    class MyController implements ContainerAwareInterface
    {
        use ContainerAwareTrait;

        protected $entityManager;

        public function __construct(Doctrine $doctrine)
        {
            $this->entityManager = $doctrine->entityManager;
        }

        public function indexAction()
        {
            ...
        }
   }

容器和手柄:

$container = new DependencyInjection\ContainerBuilder();
$container->register('context', Routing\RequestContext::class);
$container->register('matcher', Routing\Matcher\UrlMatcher::class)
             ->setArguments([$routes, new Reference('context')]);
$container->register('doctrine', Doctrine::class)
             ->setArguments($db);
...
$container->register('controller.mycontroller',MyController::class)
             ->setArguments([new Reference('doctrine')]);

$request = Request::createFromGlobals();

/**
 * @var \Symfony\Component\HttpKernel\HttpKernel $kernel
 */
$kernel = $container->get('base');
/**
 * @var \Symfony\Component\HttpFoundation\Response $response
 */
$response = $kernel->handle($request);

$response->send();
4

1 回答 1

0

我在这里找到了答案https://stackoverflow.com/a/23956418/8328328

所以不覆盖 Controller Resolver 是不行的。

于 2017-08-27T16:22:53.203 回答