laravel 拉拉维尔。替换 URL 中的 %20

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33956708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:44:51  来源:igfitidea点击:

laravel. Replace %20 in url

phplaravelurl

提问by Evaldas Butkus

I have simple problem, I have to replace %20 and other crap from URL. At the moment it looks like this http://exmaple/profile/about/Eddies%20Plumbing. As you can see it's profile link.

我有一个简单的问题,我必须从 URL 中替换 %20 和其他废话。目前看起来是这样的http://exmaple/profile/about/Eddies%20Plumbing。如您所见,它是个人资料链接。

Yes I could add str_replacevalues before every hyperlink, but I have like 10 of them and I think it's bad practice. Maybe there is better solution? What solution would you use? Thanks.

是的,我可以str_replace在每个超链接之前添加值,但我有 10 个,我认为这是不好的做法。也许有更好的解决方案?你会使用什么解决方案?谢谢。

回答by Bogdan

That is not crap, that is a valid unicode representation of a space character. And it's encoded because it's one of the characters that are deemed unsafe by RFC1738:

这不是废话,这是空格字符的有效 unicode 表示。它被编码是因为它是RFC1738认为不安全的字符之一:

All unsafe characters must always be encoded within a URL. For example, the character "#"must be encoded within URLs even in systems that do not normally deal with fragment or anchor identifiers, so that if the URL is copied into another system that does use them, it will not be necessary to change the URL encoding.

所有不安全的字符必须始终在 URL 中编码。例如,"#"即使在通常不处理片段或锚标识符的系统中,字符也必须在 URL 中编码,这样如果 URL 被复制到另一个使用它们的系统中,则无需更改 URL 编码。

So in order to have pretty URLs, you should avoid using reserved and unsafe characters which need encoding to be valid as part of a URL:

因此,为了获得漂亮的 URL,您应该避免使用需要编码才能作为 URL 一部分有效的保留字符和不安全字符:

Reserved characters:$ & + , / : ; = ? @

Unsafe characters:Blank/empty space and < > # % { } | \ ^ ~ [ ] `

保留字符:$ & + , / : ; = ? @

不安全字符:空白/空格和< > # % { } | \ ^ ~ [ ] `

Instead replace spaces with dashes, which serve the same purpose visually while being a safe character, for example look at the Stack Overflow URL for this question. The URL below looks just fine and readable without spaces in it:

取而代之的是用破折号替换空格,它们在视觉上具有相同的目的,同时又是一个安全的字符,例如查看此问题的堆栈溢出 URL。下面的 URL 看起来很好并且可读,其中没有空格:

http://exmaple/profile/about/eddies-plumbing

You can use Laravel's str_slughelper function to do the hard work for your:

您可以使用 Laravel 的str_slug辅助函数为您完成以下繁重的工作:

str_slug('Eddies Plumbing', '-'); // returns eddies-plumbing


The str_slugdoes more that replace spaces with dashes, it replaces multiple spaces with a single dash and also strips all non-alphanumeric characters, so there's no reliable way to decode it.

str_slug用破折号替换空格的作用更多,它用单个破折号替换多个空格,并去除所有非字母数字字符,因此没有可靠的方法来解码它。

That being said, I wouldn't use that approach in the first place. There are two main ways I generally use to identify a database entry:

话虽如此,我一开始不会使用这种方法。我通常使用两种主要方法来识别数据库条目:

1. Via an ID

1.通过身

The route path definition would look like this in your case:

在您的情况下,路由路径定义如下所示:

/profiles/about/{id}/{slug?} // real path "/profiles/about/1/eddies-plumbing"

The code used to identify the user would look like this User::find($id)(the slugparameter is not needed, it's just there to make the URL more readable, that's why I used the ?to make it optional).

用于识别用户的代码看起来像这样User::find($id)slug不需要参数,它只是为了使 URL 更具可读性,这就是我使用 将其?设为可选的原因)。

2. Via a slug

2. 通过 slug

The route path definition would look like this in your case:

在您的情况下,路由路径定义如下所示:

/profiles/about/{slug} // real path "/profiles/about/eddies-plumbing"

In this case I always store the slug as a column in the userstable because it's a property relevant to that user. So the retrieval process is very easy User::where('slug', $slug). Of course using str_slugto generate a valid slug when saving the user to the database. I usually like this approach better because it has the added benefit of allowing the slug to be whatever you want (not really needing to be generated from the user name). This can also allow users to choose their custom URL, and can also help with search engine optimisation.

在这种情况下,我总是将 slug 作为列存储在users表中,因为它是与该用户相关的属性。所以检索过程非常容易User::where('slug', $slug)。当然str_slug用于在将用户保存到数据库时生成有效的 slug。我通常更喜欢这种方法,因为它具有允许 slug 随心所欲的额外好处(实际上不需要从用户名生成)。这也可以让用户选择他们的自定义 URL,还可以帮助搜索引擎优化。

回答by Milan Maharjan

The links are urlencoded. Use urldecode($profileLink);to decode them.

链接是 urlencoded。使用urldecode($profileLink);来解码。

回答by nemonake

I am parsing the url tha i got in this way ->

我正在解析以这种方式获得的 url ->

    $replacingTitle = str_replace('-',' ',$title);

   <a href="example.com/category/{{ str_slug($article->title) }}/" />

回答by Raushan Singh

In your view ...

在你看来...

 <a href="{{url('your-url'.str_slug($comm->title))}}">{{$comm->title}</a>

and in controller using parsing your url as

并在控制器中使用将您的网址解析为

public function showBySlug($slug) {

    $title = str_replace('-',' ',$slug); 

    $post = Community::where('title','=',$title)->first();

    return view('show')->with(array(  
        'post' => $post,
    ));
}