108 lines
2.5 KiB
Markdown
Raw Normal View History

2019-10-29 19:27:16 +01:00
# Create a slug system
2019-10-29 20:54:19 +01:00
This guide will explain how to create a slug system for a Post, Article or any Content Type you want.
2019-10-29 19:27:16 +01:00
## Create attributes
2019-10-29 20:54:19 +01:00
To start building your slug system you need a `string` field as a **base** for your slug, in this example we will use `title`.
2019-10-29 19:27:16 +01:00
2019-10-29 20:54:19 +01:00
You will also need another `string` field that contains the slugified value of your `title`, in this case we will use `slug`.
2019-10-29 19:27:16 +01:00
![Slug fields](../assets/guides/slug/fields.png)
## Configure the layout for the content editor
Let's configure the layout of the **edit page** to make it more user friendly for the content editor.
- Click on the **Content Manager** link in the left menu.
- Then on the `Article` Content Type.
- And finally on the **Edit View** tab.
Here we will be able to setup the `slug` field.
- Click on the `slug` field.
- At the bottom of the page, edit the **placeholder** value to `Generated automatically based on the title`.
- And click **OFF** for **Editable field** option.
- Don't forget to save your updates.
2019-11-07 12:05:39 +01:00
:::: tabs
2019-10-29 19:27:16 +01:00
2019-11-07 12:05:39 +01:00
::: tab "View before"
2019-10-29 19:27:16 +01:00
2019-11-07 12:05:39 +01:00
![View before](../assets/guides/slug/layout-before.png)
2019-10-29 19:27:16 +01:00
:::
2019-11-07 12:05:39 +01:00
::: tab "View after"
2019-10-29 19:27:16 +01:00
2019-11-07 12:05:39 +01:00
![View after](../assets/guides/slug/layout-after.png)
2019-10-29 19:27:16 +01:00
:::
2019-11-07 12:05:39 +01:00
::: tab "View configuration"
2019-10-29 19:27:16 +01:00
![Edit View config](../assets/guides/slug/layout-config.png)
:::
::::
## Auto create/update the `slug` attribute
For that you will have to install `slugify` node module in your application.
When it's done, you have to update the life cycle of the **Article** Content Type to auto complete the `slug` field.
**Path —** `./api/article/models/Article.js`
2019-11-07 12:05:39 +01:00
:::: tabs
2019-10-29 19:27:16 +01:00
2019-11-07 12:05:39 +01:00
::: tab Mongoose
2019-10-29 19:27:16 +01:00
```js
const slugify = require('slugify');
module.exports = {
beforeSave: async model => {
if (model.title) {
model.slug = slugify(model.title);
}
},
beforeUpdate: async model => {
if (model.getUpdate().title) {
model.update({
slug: slugify(model.getUpdate().title),
});
}
},
};
```
:::
2019-11-07 12:05:39 +01:00
::: tab Bookshelf
2019-10-29 19:27:16 +01:00
```js
const slugify = require('slugify');
module.exports = {
beforeSave: async (model, attrs, options) => {
if (options.method === 'insert' && attrs.title) {
model.set('slug', slugify(attrs.title));
} else if (options.method === 'update' && attrs.title) {
attrs.slug = slugify(attrs.title);
}
}
}
:::
::::
## Fetch article by `slug`
Then you will have to be able to fetch your **Articles** by this slug.
You will be able to find your articles by slug with this request `GET /articles?slug=my-article-slug`
```