php - DI Container and controllers -


i followed http://fabien.potencier.org/article/50/create-your-own-framework-on-top-of-the-symfony2-components-part-1 articles, , have questions di container.

let's want fire event inside controller, how dispatcher inside controller?

i'm starting test framework through

$c->get('app')->handle($request); 

where 'app' symfony\httpkernel. how can set dependencies container? let's have view engine, defined in container

$c->register('view.engine', 'core\viewengine'); 

and want give object, or resolve object, inside controller render views. it's same problem event fire, don't have access values inside controller ... how di container supposed work in situations this?

thanks!

there different approaches. might want read through silex documentation next step. in silex, application di container. might read through introduction symfony 2 documentation.

the straight forward approach (and 1 used s2 default) inject di container controller. controller can pull out services such dispatcher needed.

a "better" approach inject dispatcher along whatever else controller needs directly controller. it's "better" because controller not need access container. it's more difficult since controller needs number of services it's job.

==============================================

how inject container in controller though?

that looking @ existing frameworks starts come in handy. remember httpkernel component , not framework. how use you.

in symfony 2 app object derived kernel , not httpkernel. kernel in turn contains instance of httpkernel instance of container.

there several approaches might take. there no single "correct" one.

if httpkernel::handleraw find:

$controller = $this->resolver->getcontroller($request)) 

you might make own controller resolver object inject container after creating controller. 1 possibility.


Comments