php 什么是蛞蝓?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19335215/
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
What is a slug?
提问by db998
I'm currently working through CodeIgniters tutorial in its fantastic documentation. However there is a term that is frequently used and it's called a "slug". I've looked around a lot to find out what the term means and I can't make sense of what it is and what it's for. Here's an example of when it's used:
我目前正在其出色的文档中学习 CodeIgniters 教程。然而,有一个经常使用的术语,它被称为“slug”。我环顾四周以找出该术语的含义,但我无法理解它是什么以及它的用途。以下是使用时的示例:
With this code you can perform two different queries. You can get all news records, or get a news item by its slug
使用此代码,您可以执行两个不同的查询。您可以获取所有新闻记录,或通过其 slug 获取新闻项目
回答by Petre P?tra?c
A slug is a part of the URL when you are accessing a resource. Say you have a URL, such as the one below, that displays all of the cars in your system:
当您访问资源时,slug 是 URL 的一部分。假设您有一个 URL,如下所示,显示系统中的所有汽车:
http://localhost/cars
When you would want to reference a particular car in your system, you would provide the following URL:
当您想在系统中引用特定汽车时,您需要提供以下 URL:
http://localhost/cars/audi-a6/
Notice how the URL is still very logical, and very SEO friendly. In terms of using the slug, that's at your own discretion. The audi-a6string above may be a unique identifier for a car in your system — let's say that you have a relational database with the following fields:
请注意 URL 仍然非常合乎逻辑,并且非常适合 SEO。在使用 slug 方面,这由您自己决定。上面的audi-a6字符串可能是您系统中汽车的唯一标识符——假设您有一个包含以下字段的关系数据库:
id
car_name
car_brand
car_unique_identifier
The field car_unique_identifier
would then be used to store the values that get displayed in the slug; in the example that I've specified above with an Audi A6 car, this is where your audi-a6string would live.
car_unique_identifier
然后该字段将用于存储显示在 slug 中的值;在我上面用奥迪 A6 汽车指定的示例中,这是您的audi-a6字符串所在的位置。
You may use it in other ways as well — for instance, if you have a postscontroller that functions like a blog. The title for a page might be the slug for it, if it is URL encoded. For our article named "Best ways to make SEO better", you may provide the following URL:
您也可以以其他方式使用它——例如,如果您有一个功能类似于博客的帖子控制器。如果页面是 URL 编码的,则页面的标题可能是它的 slug。对于我们名为“改进 SEO 的最佳方法”的文章,您可以提供以下 URL:
http://localhosts/posts/best-ways-to-make-seo-better
You would then run url_decode()
on the slug, and you would obtain the string best ways to make seo better, which you can use in order to find a post via its title.
然后url_decode()
,您将在 slug 上运行,您将获得使 seo 变得更好的字符串最佳方法,您可以使用它来通过标题查找帖子。
It doesn't need to stop there — you may decide to have multiple slugs to represent something — let's take a look at how BBC is doing it. I've taken a random article from today, which has the following URL:
不必就此止步——你可能决定用多个 slug 来代表某事——让我们来看看 BBC 是如何做的。我从今天随机拿了一篇文章,它有以下网址:
http://www.bbc.co.uk/news/world-africa-24506006
This links to an article named: African Union urges ICC to drop cases against leaders. The way that BBC are doing it is that they use the last part of the slug world-africa-24506006, which is 24506006, to identify a unique entry in their system. They then most likely use world-africato denote the category that a post belongs to (although this may only be an assumption, it's still an educated guess).
这链接到一篇名为:非洲联盟敦促国际刑事法院撤销针对领导人的案件。BBC 这样做的方式是,他们使用 slug world-africa-24506006的最后一部分,即24506006来标识其系统中的唯一条目。然后,他们很可能使用世界非洲来表示帖子所属的类别(尽管这可能只是假设,但仍然是有根据的猜测)。
Finally, let's imagine the following DB table, for research papers.
最后,让我们想象一下下面的数据库表,用于研究论文。
id
category
title
You may have an example that works like the one below.
您可能有一个与下面类似的示例。
http://localhost/papers
This URL represents all of the research papers currently in the system. You would then be able to access all of the research papers on physics via the following slug:
此 URL 代表系统中当前的所有研究论文。然后,您将能够通过以下 slug 访问所有关于物理学的研究论文:
http://localhost/papers/physics
Our slug is physics, and our database select currently looks something like:
我们的 slug 是Physics,我们的数据库选择目前看起来像:
SELECT *
FROM papers
WHERE LOWER(papers.category) = 'physics'
You may then expose the following URL:
然后,您可以公开以下 URL:
http://localhost/papers/physics/gravitation
Now our slug is composed of physicsand gravitation. Our query behind the scenes may look something like:
现在我们的 slug 是由物理和引力组成的。我们在后台的查询可能类似于:
SELECT *
FROM papers
WHERE LOWER(papers.category) = 'physics'
AND LOWER(papers.title) = 'gravitation'
This allows us to uniquely identify an entry in our system.
这使我们能够唯一地标识系统中的条目。
So we've used slugs repeatedly in order to filter out our information. In the example, when we ran the URL without any slugs:
所以我们反复使用 slug 来过滤掉我们的信息。在示例中,当我们运行没有任何 slug 的 URL 时:
http://localhost/papers
We wanted to list all of the research papers available. When we ran the URL with the physicsslug:
我们想列出所有可用的研究论文。当我们使用物理slug运行 URL 时:
http://localhost/papers/physics
We wanted to list all of the research papers on physics, thus narrowing our results. Finally, when we provided two slugs, we could uniquely identify an entry in our system.
我们想列出所有关于物理学的研究论文,从而缩小我们的结果。最后,当我们提供两个 slug 时,我们可以唯一地标识系统中的条目。
http://localhost/papers/physics/gravitation
Could we have modeled this differently? Of course! Depending on our system's requirements, we can normalise and denormalise our relational tables. We could have had a permalink system in place, so that our posts table looked like this:
我们可以以不同的方式对此进行建模吗?当然!根据我们系统的要求,我们可以规范化和非规范化我们的关系表。我们可以有一个固定链接系统,所以我们的帖子表看起来像这样:
id
title
permalink
We might then have had the following entry:
然后我们可能有以下条目:
| 20013 | Gravitation | physics-gravitation-breakthrough |
Thus exposing the URL:
从而暴露 URL:
http://localhost/papers/physics-gravitation-breakthrough
In the example above, the slug physics-gravitation-breakthroughallows us to uniquely identify a post via:
在上面的例子中,slug Physics-gravitation-breakthrough允许我们通过以下方式唯一标识一个帖子:
SELECT *
FROM papers
WHERE papers.permalink = physics-gravitation-breakthrough
回答by oluckyman
Short answer
简答
It's the what-is-a-slug
part in the URL of this question.
这what-is-a-slug
是这个问题的 URL 中的一部分。
回答by Vaibhav Singh
"slug" is totally context dependent word, but in programming or as far as this question is concerned here "slug" refers to a URL. We know that a URL can't contain every character. That's why when any post, page, controller have these typed to title so its slug is used as URL. Which might be automatically derived.
“slug”是完全依赖于上下文的词,但在编程中或就这个问题而言,“slug”指的是一个 URL。我们知道一个URL 不能包含每个字符。这就是为什么当任何帖子、页面、控制器将这些输入到标题时,它的 slug 被用作 URL。这可能是自动派生的。
So in one line, slug is a URL friendly versionof any name.
因此,总而言之,slug 是任何名称的URL 友好版本。
回答by SholleyonlineJava
All answers given above are correct but I want to answer directly to the question. Slugin the Codeigniter tutorial example refers to the URL "news". If you don't have "news" as your $slug or slug in your database and you run this url in your browser "index.php/news/view" will return error 404_page not found. I encountered this problem until I read the above answers which gave me the understanding of what Slug meant. I change my slug to news in the database table. Slug is like a pointer.
上面给出的所有答案都是正确的,但我想直接回答这个问题。 Codeigniter 教程示例中的Slug指的是 URL“ news”。如果您的数据库中没有“新闻”作为 $slug 或 slug,并且您在浏览器中运行此 url,“index.php/news/view”将返回错误 404_page not found。我遇到了这个问题,直到我阅读了上述答案,这让我了解了 Slug 的含义。我将数据库表中的 slug 更改为 news。Slug 就像一个指针。
回答by Vijay Chauhan
The following is an example of a slug:
下面是一个 slug 的例子:
<?php
function create_slug($string){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>