On Jul 28, 2011, at 9:30 AM, Wikus van de Merwe wrote:
> I want to use app-specific routes to make my application more portable. Let's
> assume that there
> is no "web2py/routes.py" file and my application name is "init". Now I
> created the "routes.py" file
> in "web2py/applications/init/" directory and defined my simple router there:
>
> my_router = dict(
> controllers = "DEFAULT",
> functions = ["about", "privacy", "api"],
> map_static = True,
> )
>
> My intention here was to make some URLs shorter:
> /init/default/about <-> /about
> /init/static/css/print.css <-> /static/css/print.css
>
> After reading the code of rewrite.py:load() I got an impression that I need
> to define this app-specific
> router inside a "routers" dictionary. It doesn't make much sense for me, but
> so be it, I added:
>
> routers = {"init":my_router}
>
> However this doesn't work for the app-specific routes in
> "web2py/applications/init/routes.py".
> On the other hand, it works when put in "web2py/routes.py" so I believe I
> didn't define the
> router correctly.
>
> As this is not documented precisely and the welcome or example apps have no
> app-level routes
> defined either could you please share some more information/examples on this?
> I've tried to figure
> out the mechanism from the code but the routes loading procedure is not the
> easiest one to follow :)
You need to have at least a base router defined in web2py/routes.py in order to
find the one in your application's directory. The one in the example file
should suffice (with the default app set to init or omitted):
routers = dict(
# base router
BASE = dict(
default_application = 'welcome',
),
)
It's the presence of a non-empty routers dict in the base routes.py that
enables the logic.
Personally, I'm of the view that it makes sense to put your app-specific
routers in the base routes.py routers dictionary, but that's up to you.