Skip to content

Commit 3f5337e

Browse files
committed
Documented the slugger
1 parent fcb2fd2 commit 3f5337e

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

components/string.rst

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,55 @@ letter A with ring above"*) or a sequence of two code points (``U+0061`` =
394394
Slugger
395395
-------
396396

397-
Document https://github.com/symfony/symfony-docs/issues/12404
397+
In some contexts, such as URLs and file/directory names, it's not safe to use
398+
any Unicode character. A *slugger* transforms a given string into another string
399+
that only includes safe ASCII characters::
400+
401+
use Symfony\Component\String\Slugger\AsciiSlugger;
402+
403+
$slugger = new AsciiSlugger();
404+
$slug = $slugger->slug('Wôrķšƥáçè ~~sèťtïñğš~~');
405+
// $slug = 'Workspace-settings'
406+
407+
The separator between words is a dash (``-``) by default, but you can define
408+
another separator as the second argument::
409+
410+
$slug = $slugger->slug('Wôrķšƥáçè ~~sèťtïñğš~~', '/');
411+
// $slug = 'Workspace/settings'
412+
413+
The slugger transliterates the original string into the Latin script before
414+
applying the other transformations. The locale of the original string is
415+
detected automatically, but you can define it explicitly::
416+
417+
// this tells the slugger to transliterate from Korean language
418+
$slugger = new AsciiSlugger('ko');
419+
420+
// you can override the locale as the third optional parameter of slug()
421+
$slug = $slugger->slug('...', '-', 'fa');
422+
423+
In a Symfony application, you don't need to create the slugger yourself. Thanks
424+
to :doc:`service autowiring </service_container/autowiring>`, you can inject a
425+
slugger by type-hinting a service constructor argument with the
426+
:class:`Symfony\\Component\\String\\Slugger\\SluggerInterface`. The locale of
427+
the injected slugger is the value of the application's
428+
:ref:`default_locale option <config-framework-default_locale>`::
429+
430+
use Symfony\Component\String\Slugger\SluggerInterface;
431+
432+
class MyService
433+
{
434+
private $slugger;
435+
436+
public function __construct(SluggerInterface $slugger)
437+
{
438+
$this->slugger = $slugger;
439+
}
440+
441+
public function someMethod()
442+
{
443+
$slug = $slugger->slug('...');
444+
}
445+
}
398446

399447
.. _`ASCII`: https://en.wikipedia.org/wiki/ASCII
400448
.. _`Unicode`: https://en.wikipedia.org/wiki/Unicode

0 commit comments

Comments
 (0)