Let’s have a quick but precise look at injecting various services in Symfony 2 today. Before going any deep, what does it mean by injecting services ? Well, if I am not so wrong, not many PHP frameworks do favour injecting services as Symfony 2 does. Injecting various services into the application is a really beautiful, cool thing, I would say. If you manage to inject services properly, you really don’t want to take care of object creations. Well, what’s injecting service actually is ? Injecting a service in Symfony 2 is, you define, a service or set of services (in PHP, classes) at the application load time, then you reuse those services as much as you want, any time during the execution, without redeclaring or creating objects of those services (classes). In other words it will help you to “standardize and centralize the way objects are constructed in your application” . There are 3 types of injections in Symfony 2, which are,
- Constructor Injection
- Setter Injection
- Property Injection
In this post, I will be discussing about the ‘Constructor Injection’, because I thought it might be useful to know how this type of injection works before knowing the remaining two.
Good, now let’s inject ‘Container‘ service (you need the most) to our custom built class called ‘TestHelper’. Before doing anything, you need to create class anywhere inside the Bundle. For now, I will be creating a folder called ‘Helper’ under ‘src/Test/HelloBundle’ package. (I have showed in detail, how to create a bundle in my previous post) .
- Create a php file called ‘TestHelper.php’ under ‘Test/HelloBundle/Helper’ location.
- Then add the correct namespace and class name (same as the file name). It’s always good to follow standards and it’ll help the journey in Symfony 2 a much easier one. Your ‘TestHelper’ class looks like this,
- Now you need to add the constructor which accepts one parameter, which is the services container. Then what I normally do is, assign that parameter to a class variable, so we can reuse the services container where ever we want. Remember, by default, services container is not widely available in any other location, than ‘Controller’ it self. But lot’s of magic in Symfony 2, happens inside the container. Now, we are in our own location called ‘Helper’ which does not have access to container. But thankful to the injection, we are gaining access to container, inside ‘TestHelper’ class in couple of minutes. So, after inserting the constructor and class variable, our modified ‘TestHelper’ class looks like this,
- So now, we have successfully created our class called ‘TestHelper’, and added the constructor to expect one parameter, which is the Container. Now, here’s the beauty. We don’t create objects such as ‘$testHelper = new TestHelper(new Container());’ anywhere in Symfony 2, by our own. Instead that, we tell Symfony 2 to do that bit for us. There’s a dedicated place in Symfony 2 to tell, inject Container into our newly created ‘TestHelper’ class. That’s the ‘config.yml’ file inside ‘app/config’ directory. Open the ‘config.yml’ file and add this piece of code at the end of the file. (Please make sure you have the correct indentations in the yaml file).
services: test_helper_service: class: Test\HelloBundle\Helper\TestHelper arguments: ["@service_container"]
- So our, modified ‘config.yml’ file looks like this,
- Now, you have successfully instructed the application to inject ‘Container’ to our newly created ‘TestHelper’. To access this service and call it’s function will be really simple from now on. Take a look at how we call this service below,
- To call a function which is inside this class will be as simple as this,
(Replace ‘testFunction’ with the name of your actual function).
In this post, we have discussed how injecting services work in Symfony 2 environment. We injected ‘Container’ to a newly created class, we declared services parameters in config.yml and accessed using the get() method. Remember, the get method is accessible via Controller or any class which extends the ContainerAware.
I hope you enjoyed this post. Feel free to bombard the comments section below with your feedback, questions, suggestions and almost everything.