Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

use persian characters as slug in laravel

I am using Laravel 8 and use cviebrock/eloquent-sluggable to make slug for products

so it works well when I use english title but whenever I use Persian for example "???" slug save as a english translate like this "tst" I change method of sluggable

after that method in config/sluggable change

'method' => function($string, $separator = '-')
{
    $_transliteration = array(
        '/?|?|?/' => 'ae',
        '/?|?/' => 'oe',
        '/ü/' => 'ue',
        '/?/' => 'Ae',
        '/ü/' => 'Ue',
        '/?/' => 'Oe',
        '/à|á|?|?|?|?|ā|?|?|ǎ/' => 'A',
        '/à|á|a|?|?|?|ā|?|?|ǎ|a/' => 'a',
        '/?|?|?|?|?/' => 'C',
        '/?|?|?|?|?/' => 'c',
        '/D|?|?/' => 'D',
        '/e|?|?/' => 'd',
        '/è|é|ê|?|ē|?|?|?|ě/' => 'E',
        '/è|é|ê|?|ē|?|?|?|ě/' => 'e',
        '/?|?|?|?/' => 'G',
        '/?|?|?|?/' => 'g',
        '/?|?/' => 'H',
        '/?|?/' => 'h',
        '/ì|í|?|?|?|ī|?|ǐ|?|?/' => 'I',
        '/ì|í|?|?|?|ī|?|ǐ|?|?/' => 'i',
        '/?/' => 'J',
        '/?/' => 'j',
        '/?/' => 'K',
        '/?/' => 'k',
        '/?|?|?|?|?/' => 'L',
        '/?|?|?|?|?/' => 'l',
        '/?|?|?|?/' => 'N',
        '/?|ń|?|ň|?/' => 'n',
        '/ò|ó|?|?|ō|?|ǒ|?|?|?|?/' => 'O',
        '/ò|ó|?|?|ō|?|ǒ|?|?|?|?|o/' => 'o',
        '/?|?|?/' => 'R',
        '/?|?|?/' => 'r',
        '/?|?|?|?|?/' => 'S',
        '/?|?|?|?|?|?/' => 's',
        '/?|?|?|?/' => 'T',
        '/?|?|?|?/' => 't',
        '/ù|ú|?|?|ū|?|?|?|?|?|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'U',
        '/ù|ú|?|?|ū|?|?|?|?|?|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
        '/Y|?|?/' => 'Y',
        '/y|?|?/' => 'y',
        '/?/' => 'W',
        '/?/' => 'w',
        '/?|?|?/' => 'Z',
        '/?|?|?/' => 'z',
        '/?|?/' => 'AE',
        '/?/' => 'ss',
        '/?/' => 'IJ',
        '/?/' => 'ij',
        '/?/' => 'OE',
        '/?/' => 'f'
    );
    $quotedReplacement = preg_quote($separator, '/');
    $merge = array(
        '/[^sp{Zs}p{Ll}p{Lm}p{Lo}p{Lt}p{Lu}p{Nd}]/mu' => ' ',
        '/[sp{Zs}]+/mu' => $separator,
        sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
    );
    $map = $_transliteration + $merge;
    unset($_transliteration);
    return preg_replace(array_keys($map), array_values($map), $string);
}

the error i have is

LogicException : Your configuration files are not serializable.

what's the solution?

question from:https://stackoverflow.com/questions/65651278/use-persian-characters-as-slug-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you can't serialise methods it is recommended you don't define methods within your configuration files. This is because if you want to later pass configurations to your queued jobs, this will not work.

You can make a helper class under e.g. your app directory:

class HelperClass
{
    public static function sluggableCustomSlugMethod($string, $separator = '-')
    {
        $_transliteration = array(
            '/?|?|?/' => 'ae',
            '/?|?/' => 'oe',
            '/ü/' => 'ue',
            '/?/' => 'Ae',
            '/ü/' => 'Ue',
            '/?/' => 'Oe',
            '/à|á|?|?|?|?|ā|?|?|ǎ/' => 'A',
            '/à|á|a|?|?|?|ā|?|?|ǎ|a/' => 'a',
            '/?|?|?|?|?/' => 'C',
            '/?|?|?|?|?/' => 'c',
            '/D|?|?/' => 'D',
            '/e|?|?/' => 'd',
            '/è|é|ê|?|ē|?|?|?|ě/' => 'E',
            '/è|é|ê|?|ē|?|?|?|ě/' => 'e',
            '/?|?|?|?/' => 'G',
            '/?|?|?|?/' => 'g',
            '/?|?/' => 'H',
            '/?|?/' => 'h',
            '/ì|í|?|?|?|ī|?|ǐ|?|?/' => 'I',
            '/ì|í|?|?|?|ī|?|ǐ|?|?/' => 'i',
            '/?/' => 'J',
            '/?/' => 'j',
            '/?/' => 'K',
            '/?/' => 'k',
            '/?|?|?|?|?/' => 'L',
            '/?|?|?|?|?/' => 'l',
            '/?|?|?|?/' => 'N',
            '/?|ń|?|ň|?/' => 'n',
            '/ò|ó|?|?|ō|?|ǒ|?|?|?|?/' => 'O',
            '/ò|ó|?|?|ō|?|ǒ|?|?|?|?|o/' => 'o',
            '/?|?|?/' => 'R',
            '/?|?|?/' => 'r',
            '/?|?|?|?|?/' => 'S',
            '/?|?|?|?|?|?/' => 's',
            '/?|?|?|?/' => 'T',
            '/?|?|?|?/' => 't',
            '/ù|ú|?|?|ū|?|?|?|?|?|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'U',
            '/ù|ú|?|?|ū|?|?|?|?|?|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
            '/Y|?|?/' => 'Y',
            '/y|?|?/' => 'y',
            '/?/' => 'W',
            '/?/' => 'w',
            '/?|?|?/' => 'Z',
            '/?|?|?/' => 'z',
            '/?|?/' => 'AE',
            '/?/' => 'ss',
            '/?/' => 'IJ',
            '/?/' => 'ij',
            '/?/' => 'OE',
            '/?/' => 'f'
        );
        $quotedReplacement = preg_quote($separator, '/');
        $merge = array(
            '/[^sp{Zs}p{Ll}p{Lm}p{Lo}p{Lt}p{Lu}p{Nd}]/mu' => ' ',
            '/[sp{Zs}]+/mu' => $separator,
            sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
        );
        $map = $_transliteration + $merge;
        unset($_transliteration);
        return preg_replace(array_keys($map), array_values($map), $string);
    }
}

Then add this in your config/sluggable:

'method' => [ AppHelperClass::class, 'sluggableCustomSlugMethod' ]

This should allow you to serialise the configuration when you need to.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...