PHP code example of fbeen / uniqueslugbundle

1. Go to this page and download the library: Download fbeen/uniqueslugbundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fbeen / uniqueslugbundle example snippets




namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Fbeen\UniqueSlugBundle\Annotation\Slug;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity(repositoryClass="App\Repository\NewsitemRepository")
 */
class Newsitem
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=64)
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     */
    private $body;

    /**
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @Slug("title")
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $slug;
    
    public function __construct()
    {
        $this->created = new \DateTime();
    }
    
    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getBody(): ?string
    {
        return $this->body;
    }

    public function setBody(string $body): self
    {
        $this->body = $body;

        return $this;
    }

    public function getCreated(): ?\DateTimeInterface
    {
        return $this->created;
    }

    public function setCreated(\DateTimeInterface $created): self
    {
        $this->created = $created;

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }
}

    /**
     * @Route("/{slug}", name="newsitem_show", methods={"GET"})
     */
    public function show(Newsitem $newsitem): Response
    {
        return $this->render('newsitem/show.html.twig', [
            'newsitem' => $newsitem,
        ]);
    }


    /**
     * @Slug("generateSlug")
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $slug;
    
    public function generateSlug()
    {
        return $this->created->format('Y-m-d') . ' ' . $this->title;
    }

    $ php bin/console make:slug Newsitem --regenerate


// App\Service\MyCustomSlugifier.php

namespace App\Service;

use Fbeen\UniqueSlugBundle\Slugifier\SlugifierInterface;

/**
 * My custom slugifier
 */
class MyCustomSlugifier implements SlugifierInterface
{
    public function slugify($text) : string
    {
        // replace non letter or digits by -
        $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

        // trim
        $text = trim($text, '-');

        // transliterate latin characters
        $text = \transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] remove', $text);

        // lowercase
        $text = strtolower($text);

        // remove unwanted characters
        $text = preg_replace('~[^-\w]+~', '', $text);

        if (empty($text))
        {
          return 'n-a';
        }

        return $text;
    }
}