문제

Similar question, which didn't solve my problem.

I want my app to have unique string identifiers (slugs) for every page, and to make URLs shorter I'm trying to hide the controller and function names from the URL. So, instead of this:

mysite.com/app/c/f/my-slug

I'd like to get this:

mysite.com/my-slug

The best I can get (using URL rewriting) is this:

mysite.com/f/my-slug

If I just set "c" as default controller and "f" as default function and try to visit app/some-slug (given that "f" can handle such slug), web2py gives an "invalid controller (my)" error.

Since I couldn't get the pattern-based URL rewriting to work (maybe because I just did it wrong), I think I would need to store each slug along with its default controller and function names in the DB and some kind of "universally default" function to handle all this. Then, if I needed to do another action to some page, I'd put the action name in front of the slug in the URL.

So... Is it feasible? Exaggerated? A terrible idea?

도움이 되었습니까?

해결책

In routes.py, you can do:

routers = dict(
    BASE = dict(
        default_application='myapp',
        default_controller='default',
        default_function='index',
        functions=['function1', 'function2', 'function3']))

In that case, /my-slug will be routed to /myapp/default/index/my-slug.

If instead you want to map different slugs to different controllers and functions, you could do that by looking up the slug in a database (or some other means of storage) and setting request.controller and request.function in a model file, but you probably don't want to have to do an extra database lookup on every request. Instead, you could use the pattern-based re-write system, but you'll have to set up patterns for every slug.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top