My application involves registering multiple users, each of whom gets their own webpage(s).
Normally the URLs would be something like: host/myapp/mycontroller/joe/home but this would entail having a separate function for each user. Another possibility would be : host/myapp/mycontroller/home/joe , which passes 'joe' as an arg to a single home function, but still has a lot of cruft for a user's home page URL.
Much nicer would be to give each user URL's like: host/joe/home
After much trial and error, I accomplished this in routes.py like this
---------- routes.py
# Remap URLS Given a URL: host/A/B/C/D
# 1) if A is one of the specific appnames, goto *web2py/applications/A/B/C/D*
# (normal web2py mapping)
# 2) if A is one of the specific page names, goto web2py/applications/myapp/myctrl/A/B/C/D
# (insert 'myapp/myctrl' into the path)
# 3) else A is a user name and B is the controller -- goto web2py/applications/myapp/B/index/A/C/D
# pass A (user) as parameter to B(ctrl)
#app names
apps = '(?P<apps>admin|welcome)'
#page names
pages = '(?P<pages>home|about|calendar|catalog|policies|register|search)'
routes_in=(
# is first an app?
('^.*:/'+apps+'/(?P<rest>.*)$',
'/\g<apps>/\g<rest>'),
# is first a pagename?
('^.*:/'+pages+'/?(?P<rest>.*)$',
'/myapp/myctrl/\g<pages>/\g<rest>'),
# else first is a user name
('^.*:/(?P<user>[^\\/\.]+)/(?P<page>[^\\/\.]+)(?P<rest>.*)$',
'/myapp/\g<page>/index/\g<user>\g<rest>'),
)
routes_out=(
('^.*:/myapp/myctrl/'+pages+'/?(?P<rest>.*)$',
'\g<pages>/\g<rest>'),
('^.*:/myapp/(?P<page>[^\\/\.]+)/index/(?P<user>[^\\/\.]+)(?P<rest>.*)$',
'\g<user>/\g<page>\g<rest>'),
)
Comments (0)