Creating Custom URLs in Your Wheels App with Routes

Today, I released a chapter in the Documentation called Using Routes.

Routes are a pretty cool feature of Wheels because they let you step outside of the URL convention that can sometimes feel like shackles.

As I outline in the new chapter, you can create a URL like this:

http://www.domain.com /user/johndoe

With this line of code in your config/routes.cfm file:

<cfset addRoute(name="userProfile", pattern="user/[username]", controller="user", action="profile")>

Read on to find out how it all works.

Comments

[Add Comment]

Raul Riera's Gravatar
Keep up the good work man hehe, if u need anything let us know. Btw is this blog running on wheels?

# Posted By Raul Riera | 10/18/08 8:13 PM

Raul Riera's Gravatar
Shouldn;t this be

http://www.domain.com/news/1/story/5

instead of

http://www.domain.com/news/story/5

I think there should be an ID for the news right?

# Posted By Raul Riera | 10/18/08 8:14 PM

Chris Peters's Gravatar
@Raul:

No, the format I have is correct.

Match this...
http://www.domain.com/news/story/5

To this...
http://www.domain.com/controller/action/id

And you get this...
controller = news
action = story
id = 5

That is the default route for Wheels.

# Posted By Chris Peters | 10/18/08 8:37 PM

Chris Peters's Gravatar
Also, this blog is running on Ray Camden's BlogCFC. There was no need to re-invent the wheel on blogging platforms. We'll see if one gets implemented in Wheels in the future... :)

# Posted By Chris Peters | 10/18/08 8:39 PM

Raul Riera's Gravatar
Oh I see, I though it was a News tha hasMany Stories. I thought they were 2 models.

# Posted By Raul Riera | 10/18/08 9:10 PM

[Add Comment]