我在我的项目中使用Symfony DIC,路由和控制器解析器,以下创建自己的PHP框架
我不明白如何在不使用控制器中的配置组件或设置容器的情况下将服务正确传输到控制器的构造函数? 路由:
$routes->add('mycontroller', new RoutingRoute('/', ['_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 DependencyInjectionContainerBuilder();
$container->register('context', RoutingRequestContext::class);
$container->register('matcher', RoutingMatcherUrlMatcher::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 SymfonyComponentHttpKernelHttpKernel $kernel
*/
$kernel = $container->get('base');
/**
* @var SymfonyComponentHttpFoundationResponse $response
*/
$response = $kernel->handle($request);
$response->send();
我 https://stackoverflow.com/a/23956418/8328328 在这里找到了答案。
所以不覆盖控制器解析器就做不到。