□未翻訳
□翻訳中
□翻訳完了(細田謙二)
■レビュー(中垣健志)
web2pyでは、コントローラのアクションを呼び出す前に、入力されるリクエストのURLパスを書き換えることができます(URLマッピング)。逆に、URL関数によって生成されたURLパスも書き換えることができます(リバースURLマッピング)。これを行う理由の1つは、古い仕様のURLを扱うためです。もう1つはパスを単純化し、短くするためです。
web2py has the ability to rewrite the URL path of incoming requests prior to calling the controller action (URL mapping), and conversely, web2py can rewrite the URL path generated by the URL function (reverse URL mapping). One reason to do this is for handling legacy URLs, another is to simplify paths and make them shorter.
web2pyは2つの異なるURLリライトシステムを内蔵しています。1つは、大抵のユースケースのための簡便なパラメータベースのシステムです。もう1つは、より複雑なケースのための、柔軟なパターンベースのシステムです。URLリライトルールを指定するには、"web2py"フォルダにてroutes.pyという名の新規ファイルを作成します(routes.pyの中身は、次の節で説明するように、どちらのリライトシステムを選択したかに依存します)。
web2py includes two distinct URL rewrite systems: an easy-to-use parameter-based system for most use cases, and a flexible pattern-based system for more complex cases. To specify the URL rewrite rules, create a new file in the "web2py" folder called routes.py (the contents ofroutes.py will depend on which of the two rewrite systems you choose, as described in the next two sections).
myappというアプリケーションを書いていて、それをデフォルトにし、アプリケーションの名前をユーザに見せるURLの一部に含めないようにしたいとします。デフォルトのコントローラはまだdefaultなので、その名前もユーザが見るURLから取り除きたいとします。routes.pyに書くものは次のようになります:
Suppose you've written an application called myapp and wish to make it the default, so that the application name is no longer part of the URL as seen by the user. 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:
1.
2.
3.
routers = dict(
BASE = dict(default_application='myapp'),
)
それだけです。このルータは十分賢く、次のようなURLを正しく扱う方法を知っています:
That's it. The router is smart enough to know how to do the right thing with URLs such as:
http://domain.com/myapp/default/myapp
http://domain.com/myapp/myapp/index
その方法は次のようになります:
Here's how:
1.
2.
3.
4.
routers = dict(
BASE = dict(default_application='myapp'),
myapp = dict(languages=['en', 'it', 'jp'], default_language='en'),
)
すると、次のような入力URLは:
Now an incoming URL like this:
http://domain.com/it/static/filename
1.
applications/myapp/static/it/filename
http://domain.com/it/static/base.css
1.
applications/myapp/static/base.css
1.
2.
3.
4.
5.
6.
7.
8.
routers = dict(
BASE = dict(
domains = {
'domain1.com' : 'app1',
'domain2.com' : 'app2',
}
),
)
1.
2.
3.
4.
5.
6.
7.
8.
routers = dict(
BASE = dict(
domains = {
'domain.com:80' : 'app/insecure',
'domain.com:443' : 'app/secure',
}
),
)
今述べたパラメータベースのシステムは、大抵のユースケースに対して満足できるものですが、もう1つのパターンベースのシステムは、より複雑なケースのためにさらなる柔軟性があります。パターンベースのシステムを使うには、ルーティングパラメータの辞書としてルータを定義する代わりに、routes_inとroutes_outという2つのリスト(またはタプル)を定義します。各タプルは2つの要素を格納します:置換されるパターンとそこに置換する文字列です。たとえば:
Although the parameter-based system just described should be sufficient for most use cases, the alternative pattern-based system provides some additional flexibility for more complex cases. To use the pattern-based system, instead of defining routers as dictionaries of routing parameters, you define two lists (or tuples) of 2-tuples, routes_in and routes_out. Each tuple contains two elements: the pattern to be replaced and the string that replaces it. For example:
1.
2.
3.
4.
5.
6.
routes_in = (
('/testme', '/examples/default/index'),
)
routes_out = (
('/examples/default/index', '/testme'),
)
これらのルーティングによって次のURLは:
With these routes, the URL:
以下のものにマッピングされます:
is mapped into:
http://127.0.0.1:8000/examples/default/index
訪問者にとって、すべてのページのURLは/testmeのように提示されます。
To the visitor, all links to the page URL looks like /testme.
パータンはPythonの正規表現と同じ構文を持っています。たとえば:
The patterns have the same syntax as Python regular expressions. For example:
1.
('.*\.php', '/init/default/index'),
これは、".php"で終わるすべてのURLがインデックスページにマッピングされます。
maps all URLs ending in ".php" to the index page.
1つのアプリケーションしか公開するつもりがない場合、アプリケーションのプレフィックスをURLから取り除きたい場合があります。これは次のようにして実現できます:
Sometimes you want to get rid of the application prefix from the URLs because you plan to expose only one application. This can be achieved with:
1.
2.
3.
4.
5.
6.
routes_in = (
('/(?P<any>.*)', '/init/\g<any>'),
)
routes_out = (
('/init/(?P<any>.*)', '/\g<any>'),
)
上記の正規表現と混ぜることができるもう1つの別の構文があります。これは、(?P<name>\w+) や \g<name>の代わりに$nameを用います。例:
There is also an alternative syntax that can be mixed with the regular expression notation above. It consists of using $name instead of (?P<name>\w+) or \g<name>. For example:
1.
2.
3.
4.
5.
6.
7.
routes_in = (
('/$c/$f', '/init/$c/$f'),
)
routes_out = (
('/init/$c/$f', '/$c/$f'),
)
これは、"/example"アプリケーションのプレフィックスをすべてのURLにおいて取り除きます。
would also eliminate the "/example" application prefix in all URLs.
$name表記を使用して、routes_inからroutes_outへ自動的にマッピングすることが可能になります。ただしこの場合、正規表現を使用することはできません。たとえば次のようになります:
Using the $name notation, you can automatically map routes_in to routes_out, provided you don't use any regular expressions. For example:
1.
2.
3.
4.
5.
routes_in = (
('/$c/$f', '/init/$c/$f'),
)
routes_out = [(x, y) for (y, x) in routes_in]
ここで複数のルーティングがある場合、最初にマッチしたURLが実行されます。もしマッチするパタンがない場合、パスはそのままになります。
If there are multiple routes, the first to match the URL is executed. If no pattern matches, the path is left unchanged.
$anythingを用いると、行の最後までのすべて(.*)にマッチさせることができます。
You can use $anything to match anything (.*) until the end of the line.
ここでは、faviconとrobotsリクエストを処理するための最小限の"routes.py"を示します:
Here is a minimal "routes.py" for handling favicon and robots requests:
1.
2.
3.
4.
5.
routes_in = (
('/favicon.ico', '/examples/static/favicon.ico'),
('/robots.txt', '/examples/static/robots.txt'),
)
routes_out = ()
さらに、複雑な例を示します。"myapp"という単一のアプリを、不必要なプレフィックスなしに公開しますが、同時に、adminとappadminとstaticも公開するという例です。
Here is a more complex example that exposes a single app "myapp" without unnecessary prefixes but also exposes admin, appadmin and static:
1.
2.
3.
4.
5.
6.
7.
8.
routes_in = (
('/admin/$anything', '/admin/$anything'),
('/static/$anything', '/myapp/static/$anything'),
('/appadmin/$anything', '/myapp/appadmin/$anything'),
('/favicon.ico', '/myapp/static/favicon.ico'),
('/robots.txt', '/myapp/static/robots.txt'),
)
routes_out = [(x, y) for (y, x) in routes_in[:-2]]
ルーティングの一般的な構文は、これまで見てきた簡単な例よりも複雑です:ここではより一般的で代表的な例を示します:
The general syntax for routes is more complex than the simple examples we have seen so far. Here is a more general and representative example:
1.
2.
3.
4.
routes_in = (
('140\.191\.\d+\.\d+:https://www.web2py.com:POST /(?P<any>.*)\.php',
'/test/default/index?vars=\g<any>'),
)
これは、httpsのPOSTリクエストを、次の正規表現にマッチしたリモートIPから、www.web2py.comというホストにマッピングします。
It maps https POST requests to host www.web2py.com from a remote IP matching the regular expression
1.
'140\.191\.\d+\.\d+'
そして、次の正規表現にマッチしたページを
requesting a page matching the regular expression
1.
'/(?P<any>.*)\.php'
次のものへとリクエストします。
into
1.
'/test/default/index?vars=\g<any>'
ここで、 \g<any>は、マッチした正規表現によって置換されます。
where \g<any> is replaced by the matching regular expression.
全般的な構文は以下のとおりです:
The general syntax is
1.
'[remote address]:[protocol]://[host]:[method] [path]'
表現全体は正規表現としてマッチするので、"."は常にエスケープされ、マッチした部分表現はすべて、Pythonの正規表現の構文に従って(?P<...>...)を用いて捉えることができます。
The entire expression is matched as a regular expression, so "." should always be escaped and any matching subexpression can be captured using (?P<...>...) according to Python regex syntax.
これにより、クライアントのIPアドレスやドメインに基づいて、また、リクエストのタイプやメソッド、パスに基づいて、リクエストを再ルーティングすることが可能になります。また、異なるバーチャルホストを異なるアプリケーションにマッピングすることも可能です。マッチした部分表現はターゲットのURLを構築するために用いることができ、結果的に、GET変数に渡すことができます。
This allows to reroute requests based on the client IP address or domain, based on the type of the request, on the method, and the path. It also allows to map different virtual hosts into different applications. Any matched subexpression can be used to built the target URL and, eventually, passed as a GET variable.
Apacheやlighttpdなどのすべての主要なWebサーバーは、URLをリライトする機能を持っています。本番環境では、それらはroutes.pyに代わる選択肢になります。いずれにせよ、アプリの内部URLをハードコードせず、URL関数でそれらを生成することを強く推奨します。これにより、必要であればルーツを変更することで、あなたのアプリケーションはよりポータブルなものになります。
All major web servers, such as Apache and lighttpd, also have the ability to rewrite URLs. In a production environment that may be an option instead of routes.py. Whatever you decide to do we strongly suggest that you do not hardcode internal URLs in your app and use the URL function to generate them. This will make your application more portable in case routes should change.
アプリケーション固有のURLリライト
Application-Specific URL Rewrite
デフォルトのアプリケーション、コントローラ、関数
Default Application, Controller, and Function
パターンベースのシステムを使うとき、デフォルトのアプリケーション、コントローラ、関数は、routes.pyで適切な値を設定することで、init、default、indexから違う名前にそれぞれ変更することができます:
When using the pattern-based system, the name of the default application, controller, and function can be changed from init, default, and index respectively to another name by setting the appropriate value in routes.py:
1.
2.
3.
default_application = "myapp"
default_controller = "admin"
default_function = "start"
注:これらの項目は、web2pyのバージョン1.83で初めて登場しました。
Note: These items first appeared in web2py version 1.83.