Symfony 4, get the root path of the project from a custom class (not a controller class)

Symfony 4, get the root path of the project from a custom class (not a controller class)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing the Project Root from Service Classes in Symfony 4

In a typical Symfony 4 application, retrieving the absolute root path of the project is trivial if you are inside a controller. You can simply inject the Kernel service and call its getProjectDir() method:

$webPath = $this->get('kernel')->getProjectDir();

However, this approach becomes awkward when you move logic out of controllers into custom utility classes, such as a Foo class located in your src/Utils directory. While passing the path explicitly to the constructor—like $faa->doSomething($webPath)—works, it couples the service's initialization tightly to the controller's knowledge of the filesystem structure. Ideally, you want the utility class to be self-contained, relying only on the application context injected by the container.

The core issue here is that getProjectDir() returns a string value, but Symfony services are often designed to be stateless or to hold their dependencies via injection. To solve this without passing raw strings through constructors, you need to inject the service that knows how to resolve the path into your utility class.

The most robust pattern is to inject the Kernel object directly as a dependency. In Symfony 4, services are automatically registered in the container based on their public classes. If your Foo class is located within the src/Utils directory and is marked as a service (either via YAML configuration or attribute), you can type-hint the Kernel interface in your constructor:

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Contracts\Service\Attribute\Required; // Optional for newer versions, but good practice

class Foo
{
    private Kernel $kernel;

    public function __construct(Kernel $kernel)
    {
        $this->kernel = $kernel;
    }

    public function doSomething(): void
    {
        // Now you can access the root path whenever needed
        $projectRoot = $this->kernel->getProjectDir();
        
        // Example usage: reading a file relative to project root
        $filePath = $projectRoot . '/config/database.yml';
        
        // Logic goes here
    }
}

By injecting Kernel, you shift the responsibility of resolving the path from the controller's local scope to the application context. The controller no longer needs to know about the filesystem layout; it simply instantiates or calls your service, and the service handles its own environment discovery.

It is important to note that injecting Kernel means your service now holds a reference to the HTTP kernel lifecycle. While this is generally acceptable for utility classes that need filesystem access during request handling, be cautious if you intend to use these services in CLI commands or console scripts outside of an active web request. The getProjectDir() method works perfectly fine in both contexts because it relies on the static path defined during project generation, not runtime HTTP state.

If your Foo class is currently registered as a non-service (e.g., instantiated manually), you can still use the same pattern by passing the Kernel instance explicitly when creating the object. However, for true separation of concerns, defining it as a service in services.yaml ensures consistent instantiation:

# config/services.yaml
services:
    App\Utils\Foo:
        arguments:
            $kernel: '@kernel'

This configuration guarantees that every time Symfony creates an instance of Foo, it receives the correct kernel instance associated with the current application environment. This approach keeps your controllers clean, focusing purely on request/response logic, while letting your utility classes manage their own dependencies and environmental context.

Our network