Vanity URLs with Playframework

1.3k Views Asked by At

I would like to create Vanity URLs for web application built ontop of Playframework.

I have tried to add a parameter for index method in main page Application, this will work if i call http://localhost:9000/?vanity_url and not for http://localhost:9000/vanity_url.

How would i achieve this? I would like users to create their own urls for the solution has to be dynamic, just like on Facebook

1

There are 1 best solutions below

3
On BEST ANSWER

In the route you would put

GET /{<[a-z]+>fanPage}/? Page.showFanPage

With this one you could obtain:

http://localhost:9000/facebook

I also recommend using the slugify method provided by play framework, usually you would both check if the slug exists in the database, and look up for slug to grab the data (title of the fanpage/who's the owner of the fan page/how many views/etc etc)

In the tables:

Name: fanpage

pageID integer or bigint
title varchar
slug varchar unique
owner integer
content text

In code:

public static void showFanPage(String fanPage) {
    // use models to look for the slug
    // grab the data
    // do what you need to do
}

I'm going on examples here on how to create the urls since I don't know what app you are building:
GET /post/slug/? Page.createPage
POST /post/slug/? Page.processPage

public static void createPage() {
         // remember to create the template
         render();
}

public static void processPage(String slugName) {
        // slugName refers to the input field
        // check if the slug exists and throw error if it does
        // process
}

(note this is just an example, I don't know what kind of application you are building) I hope this helps, let me know if this is what you meant