{"id":975,"date":"2016-11-30T22:03:11","date_gmt":"2016-12-01T03:03:11","guid":{"rendered":"http:\/\/xfloyd.net\/blog\/?p=975"},"modified":"2018-04-25T08:10:42","modified_gmt":"2018-04-25T13:10:42","slug":"service-container-delemas","status":"publish","type":"post","link":"https:\/\/xfloyd.net\/blog\/?p=975","title":{"rendered":"Service Container delemas"},"content":{"rendered":"<p>How to access &#8220;service container&#8221; in Symfony2.<br \/>\n<!--more--><\/p>\n<p>Dirty way, works anywhere:<\/p>\n<pre><code ><br \/>\nglobal $kernel;<br \/>\n$serviceContainer = $kernel->getContainer()->get('any service you want');?<br \/>\n<\/code><\/pre>\n<p>But most people says you should do it as service, which in my world is not always possible, especially using __construct injection.<\/p>\n<p>This way as <strong>setContainer<\/strong><br \/>\n<pre><code ><br \/>\nMyService:<br \/>\n    class: MyBundle\\Command\\MyCommand<br \/>\n    calls:<br \/>\n        - [setContainer, [\"@service_container\"] ]<br \/>\n<\/code><\/pre><\/p>\n<p>Or this way as <strong>__construct<\/strong> injection<br \/>\n<pre><code ><br \/>\n    TaskScheduleUtility:<br \/>\n        class:  AppBundle\\TaskScheduleEngineBundle\\Helpers\\TaskScheduleUtility<br \/>\n        arguments: [\"@doctrine.orm.entity_manager\"]<\/p>\n<p><\/code><\/pre><\/p>\n<p>You can also pass it from controller, always at your fingertips as<br \/>\n<pre><code ><br \/>\n$this->container;<br \/>\n\/\/Or better yet service container shortcut<br \/>\n$this->get();<br \/>\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to access &#8220;service container&#8221; in Symfony2.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13],"tags":[],"_links":{"self":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/975"}],"collection":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=975"}],"version-history":[{"count":10,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/975\/revisions"}],"predecessor-version":[{"id":1087,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/975\/revisions\/1087"}],"wp:attachment":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}