Symfony Services in the Real World
Now that you know what a Service is, and more importantly, how to use Services inside a Symfony application, it's time to look at a more complicated example of how you might use a combination of Services in a real world application.
In this video we are going to build on the Services we have created in the previous two videos, and configure our application to change the Service are using, depending on the environment we are working in.
For this example, we are going to look at how we can use a different version of our Placeholder Service depending on whether we are in the Production (
app.php) or the Development (
Now, this example is a little contrived - after all, it has to be something simple enough to show the concept, without being too complicated that it would take ten minutes just to walk through the set up - but it will show the code in enough depth to give you plenty of ideas as to how you might use this in your projects.
With that said, what we will be doing is showing friendly pictures of kittens when we are in the Dev environment, and boring grey placeholders when in Prod. This might be because you're wanting to pretend you're a serious business professional in front of your clients, but back in the office, it's all lolcats and nerf guns.
But joking aside (and what a joke, I'm sure your sides are hurting from such laughter), using this technique can be very beneficial in a real world project. For example, consider when you have to go outside your own code-base for some reason. Perhaps one of your Services talks to a third party API. Well, that's all good until you have to test. You can't be sending test messages to a real API for many reasons, and that creates a number of problems.
By using this method, we could swap out our Service which talks to a real API with a special Service configured purely for your Test environment. That way, all your code can stay the same, but you can mock calls to that third party API, and get the responses you require to ensure all your tests are passing.
There are many other reasons to configure this sort of setup, and hopefully after watching, you will come up with ideas for how knowing this can improve your own projects.