Laravel 与阿拉伯蛞蝓

问题描述 投票:0回答:6

我正在使用 Laravel 6 构建博客应用程序 这个应用程序的语言是阿拉伯语 但是 slug 有一个问题,Laravel 似乎不支持阿拉伯语。

有办法解决这个问题吗?

控制器

public function post($slug)
{
    $post = Post::where('slug',$slug)->first();
    return view('content.post',compact('post'));
}

储存方法

public function store(Request $request)
{
    $this->validate($request, array(
        'title'         => 'required|max:255',
        'slug'          => 'required|min:3|max:255|unique:posts',
        'body'          => 'required',
    ));
    $post = new Post;
    $post->title = $request->input('title');
    $post->slug = Str::slug($request->slug, '-');
    $post->body = $request->input('body');
    $post->save();

    return redirect('admin/posts')->with('success', 'post is successfully saved');
}

路线

Route::get('post/{slug}', 'PagesController@post')->name('post.show');
laravel laravel-5 laravel-6 laravel-6.2
6个回答
4
投票

您可以使用此功能

public function slug($string, $separator = '-') {
    if (is_null($string)) {
        return "";
    }

    $string = trim($string);

    $string = mb_strtolower($string, "UTF-8");;

    $string = preg_replace("/[^a-z0-9_\sءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]#u/", "", $string);

    $string = preg_replace("/[\s-]+/", " ", $string);

    $string = preg_replace("/[\s_]/", $separator, $string);

    return $string;
}
public function store(Request $request)
{
    $this->validate($request, array(
        'title'         => 'required|max:255',
        'slug'          => 'required|min:3|max:255|unique:posts',
        'body'          => 'required',
    ));
    $post = new Post;
    $post->title = $request->input('title');
    $post->slug = $this->slug($request->slug);
    $post->body = $request->input('body');
    $post->save();

    return redirect('admin/posts')->with('success', 'post is successfully saved');
}

它将为每种语言制作阿拉伯语或英语的鼻涕虫。 它会工作得很好。


1
投票

Laravel 对阿拉伯语等音译语言的支持不是很好。您也可以使用 PHP 的 Transliterator https://www.php.net/manual/en/transliterator.transliterate.php 来获取转写的字符串,然后再通过 Laravel 的

Str::slug
函数发送。

例如,你可以使用这样的东西:

Str::slug(transliterator_transliterate("Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", "arabic sentence you need slugged"));

请注意,您需要安装

PECL intl
才能正常工作。


1
投票

我知道 @Mohammed Aktaa 已经有一个正确的答案,但它缺少删除向后和向前斜杠的部分,这在请求具有 slug 的 get url 时可能会导致致命错误:

因此通过以下方式更正 slug 函数:

public static function slug_ar($string, $separator = '-') {
    if (is_null($string)) {
        return "";
    }
    $string = trim($string);
    $string = mb_strtolower($string, "UTF-8");
    // '/' and/or '\' if found and not remoeved it will change the get request route
    $string = str_replace('/', $separator, $string); 
    $string = str_replace('\\', $separator, $string);
    $string = preg_replace("/[^a-z0-9_\sءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]#u/", "", 
    $string);
    $string = preg_replace("/[\s-]+/", " ", $string);
    $string = preg_replace("/[\s_]/", $separator, $string);
    return $string;
  }

0
投票

你可以用这个

public static function uniqueSlug($slug,$table)
{
    $slug=self::createSlug($slug);

    $items=\DB::table($table)->select('slug')->whereRaw("slug like '$slug%'")->get();

    $count= count($items)+1;

    return $slug.'-'.$count;
}

protected static function createSlug($str)
{
    $string = preg_replace("/[^a-z0-9_\s-۰۱۲۳۴۵۶۷۸۹يءاأإآؤئبپتثجچحخدذرزژسشصضطظعغفقکكگگلمنوهی]/u", '', $str);

    $string = preg_replace("/[\s-_]+/", ' ', $string);

    $string = preg_replace("/[\s_]/", '-', $string);

    return $string;
}

0
投票

我正在使用这段 JS 代码来处理 Laravel 10

中的逻辑
        document.getElementById("postTitle").addEventListener("input", function() {
        const postSlug = document.getElementById('postSlug');

        const slugValue = this.value.trim().replace(/\s+/g, '-');

        postSlug.setAttribute('value', slugValue);
    });

它会监听“帖子标题”字段中写入的内容,并将空格转换为破折号,然后在验证后与$request一起发送


0
投票

对于 Larevel 的开发者,我更新了 @mohammed-aktaa 的 slug() 函数

它是增强阿拉伯语,具有与 Laravel Str::slug 相同的功能


/**
 *  Generates a slug for URL from a given title.
 *
 * @param string $title The title to convert to a slug.
 * @param string $separator The separator to use in the slug. Default is '-'.
 * @param string|null $language The language to use for ASCII conversion. Default is null.
 * @param array $dictionary An array of dictionary words to replace in the slug. Default is ['@' => 'at'].
 * @return string The generated slug.
 */
function slug($title, $separator = '-', $language = null, $dictionary = ['@' => 'at']) {
    if (is_null($title)) {
        return "";
    }
    
    if (!is_null($language)) {
        // The peace of code below uses Laravel \Illuminate\Support\Str class
        $title = $language ? \Str::ascii($title, $language) : $title;
    }

    // Strip whitespace (or other characters) from the beginning and end of a string
    $title = trim($title);
    // Make a string lowercase
    $title = mb_strtolower($title, "UTF-8");;

    // Replace dictionary words
    foreach ($dictionary as $key => $value) {
        $dictionary[$key] = $separator.$value.$separator;
    }
    $title = str_replace(array_keys($dictionary), array_values($dictionary), $title);

    // Remove all characters that are not the separator, letters, numbers, or whitespace
    $title = preg_replace("/[^a-z0-9_\sءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]#u/", "", $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 $title;
}
© www.soinside.com 2019 - 2024. All rights reserved.