Skip to content

Commit 0c839f5

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Minor tweaks Document the `Path::join()` method
2 parents 5422ddf + 1727c80 commit 0c839f5

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

components/filesystem.rst

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ Dealing with file paths usually involves some difficulties:
333333
- Platform differences: file paths look different on different platforms. UNIX
334334
file paths start with a slash ("/"), while Windows file paths start with a
335335
system drive ("C:"). UNIX uses forward slashes, while Windows uses backslashes
336-
by default.
336+
by default. However, Windows also accepts forward slashes, so both types of
337+
separators generally work.
337338
- Absolute/relative paths: web applications frequently need to deal with absolute
338339
and relative paths. Converting one to the other properly is tricky and repetitive.
339340

@@ -371,6 +372,45 @@ Malformed paths are returned unchanged::
371372
echo Path::canonicalize('C:Programs/PHP/php.ini');
372373
// => C:Programs/PHP/php.ini
373374

375+
Joining Paths
376+
~~~~~~~~~~~~~
377+
378+
The :method:`Symfony\\Component\\Filesystem\\Path::join` method concatenates
379+
the given paths and normalizes separators. It's a cleaner alternative to
380+
string concatenation for building file paths::
381+
382+
echo Path::join('/var/www', 'vhost', 'config.ini');
383+
// => /var/www/vhost/config.ini
384+
385+
echo Path::join('C:\\Program Files', 'PHP', 'php.ini');
386+
// => C:/Program Files/PHP/php.ini
387+
// (both forward slashes and backslashes work on Windows)
388+
389+
The ``join()`` method handles multiple scenarios correctly:
390+
391+
Empty parts are ignored::
392+
393+
echo Path::join('/var/www', '', 'config.ini');
394+
// => /var/www/config.ini
395+
396+
Leading slashes in subsequent arguments are removed::
397+
398+
echo Path::join('/var/www', '/etc', 'config.ini');
399+
// => /var/www/etc/config.ini
400+
401+
Trailing slashes are preserved only for root paths::
402+
403+
echo Path::join('/var/www', 'vhost/');
404+
// => /var/www/vhost
405+
406+
echo Path::join('/', '');
407+
// => /
408+
409+
Works with any number of arguments::
410+
411+
echo Path::join('/var', 'www', 'vhost', 'symfony', 'config', 'config.ini');
412+
// => /var/www/vhost/symfony/config/config.ini
413+
374414
Converting Absolute/Relative Paths
375415
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
376416

0 commit comments

Comments
 (0)