Dependency injection in Symfony 3.4 : check existence of a service

I am in the process of migrating an application from Symfony 2.8 to Symfony 3.4

The services are now private and therefore instead of making a direct call to the services from the container, we must use dependency injection as a workaround.

So this is the following script and i’d like to check the existence and after that call profiler service using dependency injection :

<?php

namespace DELBundleApiBundleController;

use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;

/**
 * Class EstimatePDFController
 *
 * @package DELBundleApiBundleController
 */
class EstimateController extends Controller
{
    /**
     *
     * @param Request $request Request object.
     *
     * @return Response A Response instance
     */
    public function sendAction(Request $request)
    {
        // disable debug env outputs
        if ($this->container->has('profiler')) {
            $this->container->get('profiler')->disable();
        }

        return new Response('OK');
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *