Laravel with Arabic Slugs

piv4azn7  于 2023-10-22  发布在  其他
关注(0)|答案(5)|浏览(101)

我正在使用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');
vyu0f0g1

vyu0f0g11#

您可以使用此函数

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');
}

它将为每种语言阿拉伯语或英语。它会很好地工作。

50few1ms

50few1ms2#

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才能工作。

hk8txs48

hk8txs483#

我知道@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;
  }
3zwtqj6y

3zwtqj6y4#

您可以使用此

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;
}
vc9ivgsu

vc9ivgsu5#

我正在使用这个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);
    });

它监听“posttitle”字段中的内容,并将空格转换为破折号,然后在验证后将其与**$request**一起发送

相关问题