PHP-DI / Symfony-Bridge

PHP-DI integration with Symfony
http://php-di.org/doc/frameworks/symfony2.html
Other
17 stars 11 forks source link

Bind dependencies without configuration services #7

Closed aohorodnyk closed 8 years ago

aohorodnyk commented 9 years ago

Hello @mnapoli,

I have a problem with getting dependencies without configuration php-di or symfony2 services.

Controller configured like a service and 1 dependency inject from symfony2 services, but other dependencies does not injected.

link to gist with code

What I did wrong?

Help me please

Thanks, Anton

mnapoli commented 9 years ago

Hi Anton!

You have configured the controller in Symfony config, so Symfony will be responsible to create it (and not PHP-DI).

You need to remove the service from Symfony config, i.e. remove these lines:

        <service id="a_ohorodnyk_test.example" class="AOhorodnyk\TestBundle\Controller\DefaultController">
            <argument type="service" id="test1"/>
        </service>

Now configure your controller in PHP-DI (or don't configure it if you want to use autowiring), and PHP-DI will inject dependencies.

Let me know if it's all good!

aohorodnyk commented 9 years ago

Hi Matthieu,

I tried get dependencies without any configurations, and it's doesn't work for me.

I don't understand, why I can't bind only one dependency?

Thanks

mnapoli commented 9 years ago

why I can't bind only one dependency?

Because Symfony's container and PHP-DI work separately. They share the whole instances, but not the config. So you have to build one service in PHP-DI or in Symfony but not both.

OK sorry I forgot to tell you you need also to change the routing (Resources_config_routing.xml):

    <route id="a_ohorodnyk_test_homepage" path="/hello/{name}">
        <default key="_controller">AOhorodnyk\TestBundle\Controller\DefaultController:index</default>
    </route>