Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Reference
  4. Configuring in the Kernel
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Configuration
    • Charset
    • Project Directory
    • Cache Directory
    • Build Directory
    • Log Directory
    • Container Build Time

Configuring in the Kernel

Edit this page

Configuring in the Kernel

Some configuration can be done on the kernel class itself (located by default at src/Kernel.php). You can do this by overriding specific methods of the parent Kernel class.

Configuration

In previous Symfony versions there was another configuration option to define the "kernel name", which is only important when using applications with multiple kernels. If you need a unique ID for your kernels use the kernel.container_class parameter or the Kernel::getContainerClass() method.

Charset

type: string default: UTF-8

This option defines the charset that is used in the application. This value is exposed via the kernel.charset configuration parameter and the getCharset() method.

To change this value, override the getCharset() method and return another charset:

1
2
3
4
5
6
7
8
9
10
11
12
13
// src/Kernel.php
namespace App;

use Symfony\Component\HttpKernel\Kernel as BaseKernel;
// ...

class Kernel extends BaseKernel
{
    public function getCharset(): string
    {
        return 'ISO-8859-1';
    }
}

Project Directory

type: string default: the directory of the project composer.json

This returns the absolute path of the root directory of your Symfony project, which is used by applications to perform operations with file paths relative to the project's root directory.

By default, its value is calculated automatically as the directory where the main composer.json file is stored. This value is exposed via the kernel.project_dir configuration parameter and the getProjectDir() method.

If you don't use Composer, or have moved the composer.json file location or have deleted it entirely (for example in the production servers), you can override the getProjectDir() method to return the right project directory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// src/Kernel.php
namespace App;

use Symfony\Component\HttpKernel\Kernel as BaseKernel;
// ...

class Kernel extends BaseKernel
{
    // ...

    public function getProjectDir(): string
    {
        return \dirname(__DIR__);
    }
}

Cache Directory

type: string default: $this->getProjectDir()/var/cache/$this->environment

This returns the absolute path of the cache directory of your Symfony project. It's calculated automatically based on the current environment. Data might be written to this path at runtime.

This value is exposed via the kernel.cache_dir configuration parameter and the getCacheDir() method. To change this setting, override the getCacheDir() method to return the correct cache directory.

Build Directory

type: string default: $this->getCacheDir()

This returns the absolute path of a build directory of your Symfony project. This directory can be used to separate read-only cache (i.e. the compiled container) from read-write cache (i.e. cache pools). Specify a non-default value when the application is deployed in a read-only filesystem like a Docker container or AWS Lambda.

This value is exposed via the kernel.build_dir configuration parameter and the getBuildDir() method. To change this setting, override the getBuildDir() method to return the correct build directory.

Log Directory

type: string default: $this->getProjectDir()/var/log

This returns the absolute path of the log directory of your Symfony project. It's calculated automatically based on the current environment.

This value is exposed via the kernel.logs_dir configuration parameter and the getLogDir() method. To change this setting, override the getLogDir() method to return the right log directory.

Container Build Time

type: string default: the result of executing time()

Symfony follows the reproducible builds philosophy, which ensures that the result of compiling the exact same source code doesn't produce different results. This helps checking that a given binary or executable code was compiled from some trusted source code.

In practice, the compiled service container of your application will always be the same if you don't change its source code. This is exposed via these configuration parameters:

  • container.build_hash, a hash of the contents of all your source files;
  • container.build_time, a timestamp of the moment when the container was built (the result of executing PHP's time function);
  • container.build_id, the result of merging the two previous parameters and encoding the result using CRC32.

Since the container.build_time value will change every time you compile the application, the build will not be strictly reproducible. If you care about this, the solution is to use another configuration parameter called kernel.container_build_time and set it to a non-changing build time to achieve a strict reproducible build:

  • YAML
  • XML
  • PHP
1
2
3
4
# config/services.yaml
parameters:
    # ...
    kernel.container_build_time: '1234567890'
1
2
3
4
5
6
7
8
9
10
11
<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <!-- ... -->
        <parameter key="kernel.container_build_time">1234567890</parameter>
    </parameters>
</container>
1
2
3
4
// config/services.php

// ...
$container->setParameter('kernel.container_build_time', '1234567890');
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:

Symfony 6.2 is backed by

Symfony 6.2 is backed by

Symfony Code Performance Profiling

Symfony Code Performance Profiling

Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Patrick PawseyVale, a Symfony contributor

Thanks Patrick PawseyVale for being a Symfony contributor

1 commit • 10 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia