How to Make SEO Friendly URL’s with VAPOR?

First of all you should add new Field to your Model as a string which named ‘slug’.

Entry.swift

After that, you should edit your migration. Important thing is, slug shouldn’t be ‘.required’. Otherwise, when you creating new data you have enter your slug manually every request. Also, your field should be unique. This prevents duplicate pages.

CreateEntries.swift

As a third step, you should create new file which named ‘String+Ext.swift’ or other name what you wish. Also there is link for copying for code. https://www.hackingwithswift.com/example-code/strings/how-to-convert-a-string-to-a-safe-format-for-url-slugs-and-filenames

String+Ext.swift

Lastly, you should 2 lines for your create function. Let’s say, our model name is ‘Entry’. We should create slug for new entries.

EntryController.swift

I used title Field for slug. You could use different field for your url’s. Don’t forget to ‘:slug’ parameter to add your routes. Like this:

EntryController.swift

This is the image that how I am catching slug’s.

EntryController.swift

Note: I know this is very basic tutorial for slugs but it is better than sharing nothing.

Leave a Reply