What is a slug?

前端 未结 5 1162
忘掉有多难
忘掉有多难 2020-12-12 11:09

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 looke

相关标签:
5条回答
  • 2020-12-12 11:16

    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:

    http://localhost/cars
    

    When you would want to reference a particular car in your system, you would provide the following 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-a6 string 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:

    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-a6 string would live.

    You may use it in other ways as well — for instance, if you have a posts controller 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:

    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.

    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:

    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-africa to denote the category that a post belongs to (although this may only be an assumption, it's still an educated guess).

    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:

    http://localhost/papers/physics
    

    Our slug is physics, and our database select currently looks something like:

    SELECT *
    FROM papers
    WHERE LOWER(papers.category) = 'physics'
    

    You may then expose the following URL:

    http://localhost/papers/physics/gravitation
    

    Now our slug is composed of physics and gravitation. Our query behind the scenes may look something like:

    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:

    http://localhost/papers
    

    We wanted to list all of the research papers available. When we ran the URL with the physics slug:

    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.

    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:

     http://localhost/papers/physics-gravitation-breakthrough
    

    In the example above, the slug physics-gravitation-breakthrough allows us to uniquely identify a post via:

     SELECT *
     FROM papers
     WHERE papers.permalink = physics-gravitation-breakthrough
    
    0 讨论(0)
  • 2020-12-12 11:17

    All answers given above are correct but I want to answer directly to the question. Slug in 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.

    0 讨论(0)
  • Short answer

    It's the what-is-a-slug part in the URL of this question.

    0 讨论(0)
  • 2020-12-12 11:37

    The following is an example of a 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'
      ?>
    
    0 讨论(0)
  • 2020-12-12 11:40

    "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.

    So in one line, slug is a URL friendly version of any name.

    0 讨论(0)
提交回复
热议问题