【发布时间】:2020-04-21 22:59:01
【问题描述】:
我们有来自泰米尔纳德邦的用户,看起来 Laravel 的默认 Str::slug() 无法处理以下字符:தமிழ்。它只是返回一个空白字符串。这会导致用户个人资料页面上出现 404。
我想知道如何在不影响其他 URL 的情况下解决此问题。我认为解决此问题的一种方法是覆盖默认的 Str::slug() 函数并检查原始函数是否返回空字符串。如果是这样,我可以将第一个字段注释为 $title = static::ascii($title)。然后它的作品。但我不想打扰默认的 Str::slug() 方法。同时不知道如何覆盖它。
默认 Str::slug():
public static function slug($title, $separator = '-')
{
//$title = static::ascii($title);
// Convert all dashes/underscores into separator
$flip = $separator == '-' ? '_' : '-';
$title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
// Remove all characters that are not the separator, letters, numbers, or whitespace.
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
return trim($title, $separator);
}
你是如何在你的应用中处理这个问题的? 感谢您的提前..
【问题讨论】:
标签: php laravel laravel-5 laravel-6 laravel-7