Generating doctrine slugs manually
Asked Answered
Y

3

5

I'm using sluggable behavior in my Symfony2 project, but now I would like to make many slugs for one page, based on different texts (current title, old title(s), users text from form input), and keep it in another table. And my question is - how to manually use doctrine extensions for any text? I can't find it anywhere. Perfect would be something like:

/* careful - it's not a real, working code! */
$sluggable = new DoctrineSluggable();
$slug = $sluggable->generate('My own text!');
echo $slug; // my-own-text
Yuletide answered 1/9, 2013 at 8:38 Comment(0)
Y
19

I found solution by accident here. Code:

use Gedmo\Sluggable\Util as Sluggable;    

$string = 'My own text!';
$slug = Sluggable\Urlizer::urlize($string, '-');
    if(empty($slug)) // if $string is like '=))' or 'トライアングル・サービス' an empty slug will be returned, that causes troubles and throws no exception
        echo 'error, empty slug!!!';
    else
        echo $slug;
Yuletide answered 7/9, 2013 at 10:58 Comment(0)
A
1

Find the doctrine code for generating a slug here: l3pp4rd/DoctrineExtensions. Playing around with that class could do as you desire but you will probable need to create your own service to implement an easy use as you want. See the Service Container section of the docs for more details about services.

Abdullah answered 1/9, 2013 at 10:28 Comment(1)
Thank you for answer. Well, original sluggable code is quite complicated and works good with flushing entities, but for me is too hard to use it alone for simply generating slug from text. But I found nice code in KNP bundle - I'll simply copy 2 lines with preg_replace, which work good enought for me.Yuletide
S
1

The Sluggable\Urlizer::urlize seems to replace ' with -. I had to use Sluggable\Urlizer::transliterate to be closer to the SluggableListener behaviour.

Skiles answered 21/10, 2018 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.