Skip to content

Slugging

sunrise-php edited this page Jan 7, 2019 · 1 revision

Install Sunrise Slugger

Learn more about Sunrise Slugger

composer require sunrise/slugger

Create the new dependency

Adding the following code to config/definitions.php

/**
 * Sunrise Slugger
 *
 * @link https://github.com/sunrise-php/slugger
 */
Sunrise\Slugger\SluggerInterface::class => function($container)
{
    $slugger = new Sunrise\Slugger\Slugger();

    $slugger->setTransliteratorId('de-ASCII');

    return $slugger;
},

Use Sunrise Slugger in a controller

use Sunrise\Slugger\SluggerInterface;

/**
 * @Inject
 *
 * @var SluggerInterface
 */
protected $slugger;

public function process(
    ServerRequestInterface $request,
    RequestHandlerInterface $handler) : ResponseInterface
{
    // some code
    
    if (null === $entity->getSlug()) {
        if (null !== $entity->getTitle()) {
            $slug = $this->slugger->slugify($entity->getTitle());
            $entity->setSlug($slug);
        }
    }

    // some code
}