2016-06-30 22 views
0

我使用laravel 5.2的优秀algolia/algoliasearch-laravel包。在Laravel/Algolia处理令人困惑的slu URL URL

我的一个“产品”我上传到Algolia有产品名称中斜杠:

蒂尔雄鹿羊绒围巾/披肩由埃尔金Johnstons

这是改变入使用cviebrock /口才-sluggable封装所以下面的网址:

/产品/妇女/山羊绒%20Patterned%20Scarves/TEAL-雄鹿绒-围巾++偷逐johnstons-的埃尔金

请注意++之间的围巾和偷东西。

当这上传到Algolia我得到这个:

objectID: 8122 
name: "Teal Stag Cashmere Scarf/Stole by Johnstons of Elgin" 
imgsrc: "Stag Teal Cashmere Stole (Small)_small.jpg" 
rank: 0 
url: "https://mywebsite.com/products/women/Cashmere Patterned Scarves/teal-stag-cashmere-scarfstole-by-johnstons-of-elgin" 

见algolia该网址是多么的不好吗?我已经尝试过在++中加入url,但是现在我已经迷失方向了。

+0

尝试'%2F',而不是'/'你的URL可能需要URL编码 – Farkie

+0

耶 - 这结果在Algolia结束(我使用的是preg_replace)'/ teal-stag-cashmere-scarf2fstole-by-johnstons-of-elgin' 我会看看我是否可以在将URL编码传入之前进行编码 –

+0

@Farkie - urlencoding使情况变得更糟糕lol'/ tealstagcashmerescarf2fstolebyjohnstonsofelgin' –

回答

1

解决了这个问题后,答案很简单,就是我的主要网址形成严重。我重新写的网址是利用Laravel 5.2 str_slug函数生成的方式,一切都很好再次:

/** 
* Generate a URL friendly "slug" from a given string. 
* 
* @param string $title 
* @param string $separator 
* @return string 
*/ 
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); 
}