forked from symfony/symfony-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverride_dir_structure.rst
290 lines (216 loc) · 8.39 KB
/
override_dir_structure.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
.. index::
single: Override Symfony
How to Override Symfony's default Directory Structure
=====================================================
Symfony automatically ships with a default directory structure. You can
override this directory structure to create your own. The default
directory structure is:
.. code-block:: text
your-project/
├─ assets/
├─ bin/
│ └─ console
├─ config/
├─ public/
│ └─ index.php
├─ src/
│ └─ ...
├─ templates/
├─ tests/
├─ translations/
├─ var/
│ ├─ cache/
│ ├─ log/
│ └─ ...
└─ vendor/
.. _override-cache-dir:
Override the ``cache`` Directory
--------------------------------
You can change the default cache directory by overriding the ``getCacheDir()``
method in the ``Kernel`` class of your application::
// src/Kernel.php
// ...
class Kernel extends BaseKernel
{
// ...
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-logs-dir:
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::
// src/Kernel.php
// ...
class Kernel extends Kernel
{
// ...
public function getLogDir()
{
return dirname(__DIR__).'/var/'.$this->environment.'/log';
}
}
Here you have changed the location of the directory to ``var/{environment}/log``.
.. _override-templates-dir:
Override the Templates Directory
--------------------------------
If your templates are not stored in the default ``templates/`` directory, use
the :ref:`twig.paths <config-twig-paths>` configuration option to define your
own templates directory (or directories):
.. configuration-block::
.. code-block:: yaml
# config/packages/twig.yaml
twig:
# ...
paths: ["%kernel.project_dir%/resources/views"]
.. code-block:: xml
<!-- config/packages/twig.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
http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig
http://symfony.com/schema/dic/twig/twig-1.0.xsd">
<twig:config>
<twig:path>%kernel.project_dir%/resources/views</twig:path>
</twig:config>
</container>
.. code-block:: php
// config/packages/twig.php
$container->loadFromExtension('twig', [
'paths' => [
'%kernel.project_dir%/resources/views',
],
]);
Override the Translations Directory
-----------------------------------
If your translation files are not stored in the default ``translations/``
directory, use the :ref:`framework.translator.paths <reference-translator-paths>`
configuration option to define your own translations directory (or directories):
.. configuration-block::
.. code-block:: yaml
# config/packages/translation.yaml
framework:
translator:
# ...
paths: ["%kernel.project_dir%/i18n"]
.. code-block:: xml
<!-- config/packages/translation.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
http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig
http://symfony.com/schema/dic/twig/twig-1.0.xsd">
<framework:config>
<framework:translator>
<framework:path>%kernel.project_dir%/i18n</framework:path>
</framework:translator>
</framework:config>
</container>
.. code-block:: php
// config/packages/translation.php
$container->loadFromExtension('framework', [
'translator' => [
'paths' => [
'%kernel.project_dir%/i18n',
],
],
]);
.. _override-web-dir:
.. _override-the-web-directory:
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 ``var`` directory is still correct in your
``index.php`` front controller. If you simply renamed the directory, you're
fine. But if you moved it in some way, you may need to modify these paths inside
those files::
require_once __DIR__.'/../path/to/vendor/autoload.php';
You also need to change the ``extra.public-dir`` option in the
``composer.json`` file:
.. code-block:: json
{
"...": "...",
"extra": {
"...": "...",
"public-dir": "my_new_public_dir"
}
}
.. 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
<<<<<<< HEAD
the ``public`` dir in your project.
=======
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:
.. configuration-block::
.. code-block:: yaml
# app/config/config.yml
# ...
assetic:
# ...
read_from: '%kernel.project_dir%/../public_html'
.. code-block:: xml
<!-- 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
http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/assetic
http://symfony.com/schema/dic/assetic/assetic-1.0.xsd">
<!-- ... -->
<assetic:config read-from="%kernel.project_dir%/../public_html" />
</container>
.. code-block:: php
// 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:
.. code-block:: terminal
$ php bin/console cache:clear --env=prod
$ php bin/console assetic:dump --env=prod --no-debug
>>>>>>> 3.4
Override the ``vendor`` Directory
---------------------------------
To override the ``vendor`` directory, you need to define the ``vendor-dir``
option in your ``composer.json`` file like this:
.. code-block:: json
{
"config": {
"bin-dir": "bin",
"vendor-dir": "/some/dir/vendor"
},
}
.. 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.