[I'm reposting this message from a while back, because the new release 1.92.1
contains it for the first time. It describes some simple use cases for the new
URL router. Note that there are still some things that you'll need the existing
regex-based router for, but for most cases the new one should work and be
easier to configure.]
Suppose you've written an app, named it 'myapp', and want to make it the
default, with its name always removed. Your default controller is still
'default', and you want to remove its name from user-visible URLs as well.
Here's what you put in routes.py:
routers = dict(
BASE = dict( default_application='myapp' ),
)
That's it. And it's smart enough to know how to do the right thing with URLs
like:
http://domain.com/myapp/default/myapp
or http://domain.com/myapp/myapp/index
...where normal shortening would be ambiguous.
If you have two applications, myapp and myapp2, you'll get the same effect, and
additionally myapp2's default controller will be stripped from the URL whenever
it's safe (which is mostly all the time).
Another case. Suppose you want to support URL-based languages, where your URLs
look like this:
http://myapp/en/some/path
or (rewritten)
http://en/some/path
Here's how:
routers = dict(
BASE = dict( default_application='myapp' ),
myapp = dict( languages=['en', 'it', 'jp'], default_language='en' ),
)
Now an incoming URL like this:
http:/domain.com/it/some/path
will be routed to /myapp/some/path, and request.uri_language will be set to
'it', so you can force the translation. You can also have language-specific
static files.
http://domain.com/it/static/filename
will be mapped to:
applications/myapp/static/it/filename
...if that file exists. If it doesn't, then URLs like:
http://domain.com/it/static/base.css
...will still map to:
applications/myapp/static/base.css
(because there is no static/it/base.css)
So you can now have language-specific static files, including images, if you
need to.
Domain mapping is supported as well.
routers = dict(
BASE = dict(
domains = {
'domain1.com' : 'app1',
'domain2.com' : 'app2',
}
),
)
does what you'd expect.
routers = dict(
BASE = dict(
domains = {
'domain.com:80' : 'app/insecure',
'domain.com:443' : 'app/secure',
}
),
)
...maps http://domain.com accesses to app's controller named 'insecure', while
https accesses go to the 'secure' controller. Or you can map different ports to
different apps, in the obvious way.
There's more, but mostly everything happens automatically, and there's no need
to dig into the details of configuration unless there's some non-standard thing
you need. There's a bit more documentation in router.example.py.