我控制器中的独立 Symfony DIC
Standalone Symfony DIC in my controllers
我在 Create your own PHP Framework
之后的项目中使用 Symfony DIC、Routing 和 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();
我在这里找到了答案 。
所以如果不覆盖 Controller Resolver 是做不到的。
我在 Create your own PHP Framework
之后的项目中使用 Symfony DIC、Routing 和 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();
我在这里找到了答案 。
所以如果不覆盖 Controller Resolver 是做不到的。