My favorites | Sign in
Logo
                
Search
for
Updated Jun 21, 2008 by xeonchen
Labels: Featured, Phase-Design, Phase-Implementation
UrlRouting  
Introduction to the URL routing in GAEO.

Introduction

GAEO routes your url in 3 ways by default:

  1. / will be mapped to {'controller':'welcome', 'action':'index'}
  2. /foo/bar will be mapped to {'controller':'foo', 'action': 'bar'}
  3. /foo will be mapped to {'controller':'foo', 'action':'index'}

However, you can use gaeo.dispatch.route.Router to add routing rules. This page will introduce you how to use the Router and how to write your rules.

How-to

The routing rules should be determined first before the request be processed. I suggest you to connect your rule in the main.py (or, your main handler) like this:

# import the Router class
from gaeo.dispatch.router import Router
...

def initRoutes():
  """ Initialize the URL routing rules """
  r = Router()  # get the Router instance (it's a singleton).

  # map the /signup to the appropriate controller/action
  r.connect('/signup', controller='account', action='signup')
  
  # another rule
  r.connect('/user/:id/:action', controller='user')

  # map the pattern `/foo/bar/3` to FooController, bar method, and 
  # add a parameter id=3
  r.connect('/:controller/:action/:id')
  ...

NOTE that the Router will match the pattern in the order you connected. In the above sample, if you put

r.connect('/:controller/:action/:id')

before the

r.connect('/user/:id/:action', controller='user')

then, the url /user/1234/edit will be mapped to {'controller': 'user', 'action': '1234', 'id': 'edit'} and you will get a misroute.


Comment by gregor007, Jul 20, 2008

Being a 'Rails Refugee', I like your approach. This is part of my urls.py file for a Django app:

(r'^blog/item/(\d+)/$', 'item.show'), # one Item (r'^blog/items/(-\w?+)/$', 'item.show'), # Items with Tag string (r'^blog/edit/$', 'item.edit'), # new Item (r'^blog/edit/(\d+)/$', 'item.edit'), # edit Item (r'^blog/delete/(\d+)/$', 'item.delete'), (r'^blog/item/(\d+)/comments/$', 'comment.show'), # all for an Item (r'^blog/item/(\d+)/comment/(\d+)$', 'comment.show'), # Item, Comment (r'^blog/item/(\d+)/comment/edit/$', 'comment.edit'), # new Comment (r'^blog/item/(\d+)/comment/edit/(\d+)/$', 'comment.edit'), # edit Comment (r'^blog/item/(\d+)/comment/delete/(\d+)$', 'comment.delete'), (r'^index/$', 'item.show'), # all Items (r'^blog/index/$', 'item.show'), # all Items (r'^blog/items/$', 'item.show'), # all Items (r'^$', 'item.show'), # root url (r'^.$', 'item.show'), # fall-through

Notice there are many complex situations that need to be handled, and notice also that almost all of the REST use cases can be handled with only three actions: SHOW, EDIT, and DELETE, based on a polymorphic approach of reading the arguments for the incoming request.

Bottom line: I vote for a RESTful approach of a very few controller actions (I use three, Rails uses seven), and the urls.py idea is actually very compact when you consider the variety of cases to handle.

Keep on going!

Comment by gregor007, Jul 20, 2008

Wow! Please take a moment and re-format that last comment - I did not realize my like breaks would not come through, and I don't know where the bold came from...

Comment by cp.ambrosio, Jul 15, 2009

Simple simple URL routing, i love this. I wish http request methods can be matched too. Our approaches are the same on URL routing, only mine's on PHP. Great work. :D

Regards


Sign in to add a comment
Hosted by Google Code