Skip to content

Commit cb2294e

Browse files
committed
minor #21164 [Filesystem] Document the Path::join() method (OskarStark)
This PR was merged into the 6.4 branch. Discussion ---------- [Filesystem] Document the `Path::join()` method Fix #21163 Commits ------- 9ef642b Document the `Path::join()` method
2 parents 16f868c + 9ef642b commit cb2294e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

components/filesystem.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,44 @@ Malformed paths are returned unchanged::
359359
echo Path::canonicalize('C:Programs/PHP/php.ini');
360360
// => C:Programs/PHP/php.ini
361361

362+
Joining Paths
363+
~~~~~~~~~~~~~
364+
365+
The :method:`Symfony\\Component\\Filesystem\\Path::join` method concatenates
366+
the given paths and normalizes separators. It's a cleaner alternative to
367+
string concatenation for building file paths::
368+
369+
echo Path::join('/var/www', 'vhost', 'config.ini');
370+
// => /var/www/vhost/config.ini
371+
372+
echo Path::join('C:\\Program Files', 'PHP', 'php.ini');
373+
// => C:/Program Files/PHP/php.ini
374+
375+
The method handles multiple scenarios correctly:
376+
377+
- Empty parts are ignored::
378+
379+
echo Path::join('/var/www', '', 'config.ini');
380+
// => /var/www/config.ini
381+
382+
- Leading slashes in subsequent arguments are removed::
383+
384+
echo Path::join('/var/www', '/etc', 'config.ini');
385+
// => /var/www/etc/config.ini
386+
387+
- Trailing slashes are preserved only for root paths::
388+
389+
echo Path::join('/var/www', 'vhost/');
390+
// => /var/www/vhost
391+
392+
echo Path::join('/', '');
393+
// => /
394+
395+
- Works with any number of arguments::
396+
397+
echo Path::join('/var', 'www', 'vhost', 'symfony', 'config', 'config.ini');
398+
// => /var/www/vhost/symfony/config/config.ini
399+
362400
Converting Absolute/Relative Paths
363401
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
364402

0 commit comments

Comments
 (0)