@@ -337,7 +337,8 @@ Dealing with file paths usually involves some difficulties:
337
337
- Platform differences: file paths look different on different platforms. UNIX
338
338
file paths start with a slash ("/"), while Windows file paths start with a
339
339
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.
341
342
- Absolute/relative paths: web applications frequently need to deal with absolute
342
343
and relative paths. Converting one to the other properly is tricky and repetitive.
343
344
@@ -375,6 +376,45 @@ Malformed paths are returned unchanged::
375
376
echo Path::canonicalize('C:Programs/PHP/php.ini');
376
377
// => C:Programs/PHP/php.ini
377
378
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
+
378
418
Converting Absolute/Relative Paths
379
419
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
380
420
0 commit comments