Skip to content

Commit 1727c80

Browse files
committed
Merge branch '7.3' into 7.4
* 7.3: Minor tweaks Document the `Path::join()` method
2 parents 71b1c5e + 5d8e527 commit 1727c80

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
@@ -337,7 +337,8 @@ Dealing with file paths usually involves some difficulties:
337337
- Platform differences: file paths look different on different platforms. UNIX
338338
file paths start with a slash ("/"), while Windows file paths start with a
339339
system drive ("C:"). UNIX uses forward slashes, while Windows uses backslashes
340-
by default.
340+
by default. However, Windows also accepts forward slashes, so both types of
341+
separators generally work.
341342
- Absolute/relative paths: web applications frequently need to deal with absolute
342343
and relative paths. Converting one to the other properly is tricky and repetitive.
343344

@@ -375,6 +376,45 @@ Malformed paths are returned unchanged::
375376
echo Path::canonicalize('C:Programs/PHP/php.ini');
376377
// => C:Programs/PHP/php.ini
377378

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

0 commit comments

Comments
 (0)