How to Override Symfony's default Directory Structure
Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
Symfony automatically ships with a default directory structure. You can override this directory structure to create your own. The default directory structure is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
your-project/
├─ app/
│ ├─ config/
│ ├─ Resources/
│ │ └─ views/
│ └─ ...
├─ bin/
│ └─ ...
├─ src/
│ └─ ...
├─ tests/
│ └─ ...
├─ var/
│ ├─ cache/
│ ├─ logs/
│ └─ ...
├─ vendor/
│ └─ ...
└─ web/
├─ app.php
└─ ...
Override the cache
Directory
You can change the default cache directory by overriding the getCacheDir()
method
in the AppKernel
class of your application:
1 2 3 4 5 6 7 8 9 10 11 12
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
// ...
public function getCacheDir()
{
return dirname(__DIR__).'/var/'.$this->environment.'/cache';
}
}
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 logs
Directory
Overriding the logs
directory is the same as overriding the cache
directory. The only difference is that you need to override the getLogDir()
method:
1 2 3 4 5 6 7 8 9 10 11 12
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
// ...
public function getLogDir()
{
return dirname(__DIR__).'/var/'.$this->environment.'/logs';
}
}
Here you have changed the location of the directory to var/{environment}/logs
.
Override the Templates Directory
If your templates are not stored in the default app/Resources/views/
directory, use the twig.default_path configuration option to
define your own templates directory (use twig.paths for multiple directories):
1 2 3 4
# app/config/config.yml
twig:
# ...
default_path: "%kernel.project_dir%/templates"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!-- app/config/config.xml -->
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig
https://symfony.com/schema/dic/twig/twig-1.0.xsd">
<twig:config>
<twig:default-path>%kernel.project_dir%/templates</twig:default-path>
</twig:config>
</container>
1 2 3 4
// app/config/config.php
$container->loadFromExtension('twig', [
'default_path' => '%kernel.project_dir%/templates',
]);
Override the web
Directory
If you need to rename or move your web
directory, the only thing you
need to guarantee is that the path to the var
directory is still correct
in your app.php
and app_dev.php
front controllers. 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/app/autoload.php';
You also need to change the extra.symfony-web-dir
option in the
composer.json
file:
1 2 3 4 5 6 7
{
"...": "...",
"extra": {
"...": "...",
"symfony-web-dir": "my_new_web_dir"
}
}
Tip
Some shared hosts have a public_html
web directory root. Renaming
your web directory from web
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 web
in your project.
Note
If you use the AsseticBundle, you need to configure the read_from
option
to point to the correct web
directory:
1 2 3 4 5 6
# app/config/config.yml
# ...
assetic:
# ...
read_from: '%kernel.project_dir%/../public_html'
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:assetic="http://symfony.com/schema/dic/assetic"
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
http://symfony.com/schema/dic/assetic
https://symfony.com/schema/dic/assetic/assetic-1.0.xsd">
<!-- ... -->
<assetic:config read-from="%kernel.project_dir%/../public_html"/>
</container>
1 2 3 4 5 6 7
// app/config/config.php
// ...
$container->loadFromExtension('assetic', [
// ...
'read_from' => '%kernel.project_dir%/../public_html',
]);
Now you just need to clear the cache and dump the assets again and your application should work:
1 2
$ php bin/console cache:clear --env=prod
$ php bin/console assetic:dump --env=prod --no-debug
Override the vendor
Directory
To override the vendor
directory, you need to introduce changes in the
app/autoload.php
and composer.json
files.
The change in the composer.json
will look like this:
1 2 3 4 5 6
{
"config": {
"bin-dir": "bin",
"vendor-dir": "/some/dir/vendor"
},
}
Then, update the path to the autoload.php
file in app/autoload.php
:
1 2 3 4
// app/autoload.php
// ...
$loader = require '/some/dir/vendor/autoload.php';
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.