1

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

n002213f
  • 7,805
  • 13
  • 69
  • 105

1 Answers1

7

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

allenskd
  • 1,795
  • 2
  • 21
  • 33
  • I would like users to create custom urls to point their profile, e.g. `http://www.example.com/allenskd`. `allenskd` would be unique to each user and be used to retrieve their user profile. From the docs `sluggify` wouldn't work since its _purely_ for **[SEO](http://stackoverflow.com/questions/4433620/play-framework-how-do-i-lookup-an-item-from-a-slugify-url)** The regex solution works but now need to checks for urls like controller urls like `/admin/` – n002213f Feb 14 '11 at 01:23
  • 1
    It's a common problem in the framework since Play! will hit an instant match and load the route without considering other contenders, usually I avoid this problem by just organizing routes by priority (admin routes first, etc), that's the drawback I saw using Play!, I don't know if they will ever fix it too on how the router manages the routes, and yes, all clear urls(pretty urls, however you name it) are purely for SEO, it works if the user wants to get creative (but it depends on your application) – allenskd Feb 14 '11 at 01:33
  • in `routes` file, `This file defines all application routes (Higher priority routes first)`, placing the route at the bottom solves the challenge in the above comment. – n002213f Feb 14 '11 at 01:34