How to Override Symfony’s default Directory Structure

How to Override Symfony’s default Directory Structure

Symfony applications have the following default directory structure, but you can override it to create your own structure:

  1. your-project/
  2. ├─ assets/
  3. ├─ bin/
  4. └─ console
  5. ├─ config/
  6. ├─ public/
  7. └─ index.php
  8. ├─ src/
  9. └─ ...
  10. ├─ templates/
  11. ├─ tests/
  12. ├─ translations/
  13. ├─ var/
  14. ├─ cache/
  15. ├─ log/
  16. └─ ...
  17. └─ vendor/

Override the Configuration Directory

The configuration directory is the only one which cannot be overridden in a Symfony application. Its location is hardcoded as the config/ directory at your project root directory.

Override the Cache Directory

You can change the default cache directory by overriding the getCacheDir() method in the Kernel class of your application:

  1. // src/Kernel.php
  2. // ...
  3. class Kernel extends BaseKernel
  4. {
  5. // ...
  6. public function getCacheDir()
  7. {
  8. return dirname(__DIR__).'/var/'.$this->environment.'/cache';
  9. }
  10. }

In this code, $this->environment is the current environment (i.e. dev). In this case you have changed the location of the cache directory to var/{environment}/cache/.

Caution

You should keep the cache directory different for each environment, otherwise some unexpected behavior may happen. Each environment generates its own cached configuration files, and so each needs its own directory to store those cache files.

Override the Log Directory

Overriding the var/log/ directory is the same as overriding the var/cache/ directory. The only difference is that you need to override the getLogDir() method:

  1. // src/Kernel.php
  2. // ...
  3. class Kernel extends BaseKernel
  4. {
  5. // ...
  6. public function getLogDir()
  7. {
  8. return dirname(__DIR__).'/var/'.$this->environment.'/log';
  9. }
  10. }

Here you have changed the location of the directory to var/{environment}/log/.

Override the Templates Directory

If your templates are not stored in the default templates/ directory, use the twig.default_path configuration option to define your own templates directory (use twig.paths for multiple directories):

  • YAML

    1. # config/packages/twig.yaml
    2. twig:
    3. # ...
    4. default_path: "%kernel.project_dir%/resources/views"
  • XML

    1. <!-- config/packages/twig.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:twig="http://symfony.com/schema/dic/twig"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/twig
    9. https://symfony.com/schema/dic/twig/twig-1.0.xsd">
    10. <twig:config>
    11. <twig:default-path>%kernel.project_dir%/resources/views</twig:default-path>
    12. </twig:config>
    13. </container>
  • PHP

    1. // config/packages/twig.php
    2. $container->loadFromExtension('twig', [
    3. 'default_path' => '%kernel.project_dir%/resources/views',
    4. ]);

Override the Translations Directory

If your translation files are not stored in the default translations/ directory, use the framework.translator.default_path configuration option to define your own translations directory (use framework.translator.paths for multiple directories):

  • YAML

    1. # config/packages/translation.yaml
    2. framework:
    3. translator:
    4. # ...
    5. default_path: "%kernel.project_dir%/i18n"
  • XML

    1. <!-- config/packages/translation.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:twig="http://symfony.com/schema/dic/twig"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/twig
    9. https://symfony.com/schema/dic/twig/twig-1.0.xsd">
    10. <framework:config>
    11. <framework:translator>
    12. <framework:default-path>%kernel.project_dir%/i18n</framework:default-path>
    13. </framework:translator>
    14. </framework:config>
    15. </container>
  • PHP

    1. // config/packages/translation.php
    2. $container->loadFromExtension('framework', [
    3. 'translator' => [
    4. 'default_path' => '%kernel.project_dir%/i18n',
    5. ],
    6. ]);

Override the Public Directory

If you need to rename or move your public/ directory, the only thing you need to guarantee is that the path to the vendor/ directory is still correct in your index.php front controller. If you renamed the directory, you’re fine. But if you moved it in some way, you may need to modify these paths inside those files:

  1. require_once __DIR__.'/../path/to/vendor/autoload.php';

You also need to change the extra.public-dir option in the composer.json file:

  1. {
  2. "...": "...",
  3. "extra": {
  4. "...": "...",
  5. "public-dir": "my_new_public_dir"
  6. }
  7. }

Tip

Some shared hosts have a public_html/ web directory root. Renaming your web directory from public/ to public_html/ is one way to make your Symfony project work on your shared host. Another way is to deploy your application to a directory outside of your web root, delete your public_html/ directory, and then replace it with a symbolic link to the public/ dir in your project.

Override the Vendor Directory

To override the vendor/ directory, you need to define the vendor-dir option in your composer.json file like this:

  1. {
  2. "config": {
  3. "bin-dir": "bin",
  4. "vendor-dir": "/some/dir/vendor"
  5. }
  6. }

Tip

This modification can be of interest if you are working in a virtual environment and cannot use NFS - for example, if you’re running a Symfony application using Vagrant/VirtualBox in a guest operating system.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.