@@ -333,7 +333,8 @@ Dealing with file paths usually involves some difficulties:
333
333
- Platform differences: file paths look different on different platforms. UNIX
334
334
file paths start with a slash ("/"), while Windows file paths start with a
335
335
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.
337
338
- Absolute/relative paths: web applications frequently need to deal with absolute
338
339
and relative paths. Converting one to the other properly is tricky and repetitive.
339
340
@@ -371,6 +372,45 @@ Malformed paths are returned unchanged::
371
372
echo Path::canonicalize('C:Programs/PHP/php.ini');
372
373
// => C:Programs/PHP/php.ini
373
374
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
+
374
414
Converting Absolute/Relative Paths
375
415
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
376
416
0 commit comments