routing.yml について: 基本と応用
- Date
- 2007-07-12 (Thu)
- Category
- symfony
最近 Symfony framework で開発するようになりました。まだ使いこなしていない部分も数多くありますが、とても便利です。
中でも今日は Askeet チュートリアル 4 日目: リファクタリング などで紹介されている routing.yml についてすこし解説しておきたいと思います。
基本
routing.yml はアプリケーション内での URL の規約を設定するファイルです。以下のような特徴があります。
- まず mod_rewrite かまたはそれに準ずるものがホストとなる HTTP Server にインストールされていて、正しく設定されている事が必要です。dev 環境では動いても、prod 環境では動かない、(というか HTTP Server 独自の 404 File Not Found がでる)と言う場合は RewriteBase を設定してうまくいくことが多いようです(Apache の場合)。例えば、httpd.conf の Alias をつかって http://example.com/askeet/ みたいな形で site root でないところを Symfony の web directory につないだ時には、当たり前かもしれませんが RewriteBase の設定が必要です。
- routing.yml 抜きのまっさらな状態では、Symfony の frontend controller は Symfony web root 以下の URL 文字列を /(forward slash)で区切ります。それの奇数番目を key に、偶数番目を value にした array を作って、それを以降のアクションなどに Request Parameters として渡します。
つまり、もし http://example.com/askeet に Symfony web directory が map してあって、ユーザが http://example.com/askeet/foo/bar/hoge/huga/123/456 とリクエストしてきた場合は、[ foo => bar, hoge => huga, 123 => 456 ] という風なリクエストが出来るわけです。 - デフォルトで、4つ、ルールが書かれています。これらは、したければ自分で変更したってかまいません。そしてこれらには順番には意味があります(順番は後述)。
- Symfony の frontend controller は、引数(つまり directory 区切り記号)の数をかぞえているようです。数とパターンが両方一致した、上から数えて一番最初のルールを適用するようです。
- パターンにおいて、文字列はそのままの一致。:label のように : (colon) で始めると、"label" を key とし、ユーザの送った内容を value とする array をつくり request parameter に保持されます。* (asterisk) はワイルドカードで、それのあるエントリでは、引数の数は関係なく、* 以降はあってもなくても関係なくなります。また * 以降の値は基本通り、奇数番目が key に、偶数番目が value になる request parameters を作ります。
例えばでフォルトエントリの一つの以下を参考にすると…default_symfony: url: /symfony/:action/* param: { module: default }- Symfony web root より最初の引数が symfony という値で、
- 第二の奇数をアクションとして
- それ以降の値は奇数/偶数→key/value
- :module と :action だけは特殊なラベルで、他には使えないようです。
応用
リクエストの時、
- 何も値がなかったら、全ての値を返し、
- 引数が2つ以上あったら、最初の2つをキーにした値を返し、
- それ以外は 404
とかはよくありそうな話です。先ほどの例と同じく、http://example.com/askeet に symfony web directory がmap されているとしましょう。まずは正解から
part:
url: /api/list/:id1/:id2/*
param: { module: api, action: list }
error:
url: /api/list/:id1/*
param: { module: api, action: list }
all:
url: /api/list/*
param: { module: api, action: list }
大事なのは api_list_error の項目が api_list よりも後に定義されているという事ですね。あと error と all の両方のエントリの最後が * で終わっている事。そうしないと、例えば http://example.com/askeet で来たのは拾えても、 http://example.com/askeet/ で来たのは拾えません。
そして受け取るアクションでは、以下のようなコードを書くといいかもしれません。
$req1 = $this->getRequestParameter('id1');
$req2 = $this->getRequestParameter('id2');
switch (true)
{
case (is_null($req1) && is_null($req2)):
$result = "all";
break;
case (isset($req1) && isset($req2)):
$result = "partial select";
break;
default:
$this->forward404();
break;
}
まとめ
routing.yml について解説しました。順番が大事であるという事、* をつける事の有無などの考察は何となくわかっていても、書き出してみる事で自分中でもすっきりまとまりました。
参考
- The Definitive Guide to symfony Chapter 9 - Links And The Routing System
- branches/1.0/lib/controller/sfRouting.class.php
しっかり読み込んでないけれど、504 行目から始まる parse という method がここで解説している処理部分の様子。
Comment:20
- UGG:2011-08-10 (Wed) 21:31
-
An update to a favorite silhouette, the UGG Boots for Women is one of our new surf-inspired boots. Wrapped lacing gives this causal boot some edge while staying true to its surf roots. UggA molded rubber outsole and sheepskin lined uppers make the Surf Collection one of our most comfortable collections.
- Juicy Handbag:2011-09-07 (Wed) 23:15
-
Hi, thank you so a great deal for these ideas! - Prada Purses:2011-09-07 (Wed) 23:16
-
Positive glad that I navigated in your web page by accident. I'll be subscribing to your feed in order that I can get the latest updates. Appreciate all of the details right here - Coach Purses:2011-09-07 (Wed) 23:16
-
This can be a great website write-up and I defer to you what you've said below. I've previously subscribed for a RSS feed in Firefox and are going to be your regular reader. Many thanks for the time in writing the submit. - diablo 3 gold:2011-09-14 (Wed) 02:44
-
leather strap the whole air very stylish rubber chain belt or crocodile leather fans watch the most admired I heard so .
- replica watches:2011-09-15 (Thu) 04:27
-
before making your final decisionwe seen anime or game designers dealtrims the edges and bottom of the.
- watches replica:2011-09-15 (Thu) 20:46
-
style it It is ideal for makingedition is worth buying You can match yourquality hardware with the Hermes H.
- replica watches:2011-09-20 (Tue) 04:02
-
fashionable today In fact many womensetting up the foundations of a watcheffect If you are caught.
- tiger shoes :2011-11-10 (Thu) 03:21
-
the Onitsuka Tiger Tokidoki Mex lo shoes for men.
- winter clothes:2011-12-01 (Thu) 20:59
-
Really make the person touched, admire. Good article
- watches for sale:2011-12-02 (Fri) 04:17
-
preferred choice for special events such as weddings, christening, dinners or even a big night out.
Whenever you are carrying a stunning silver clutch as an accessory this will add the finishing touch to a party dress, while a sequin or sparkly bag is actually great for Christmas Parties or special evening on the town.
- Replica patek philippe:2011-12-02 (Fri) 04:17
-
oulder Bags are available in a wide selection of designs and sizes and are probably the most popular types of bags for day-to-day use either for work or shopping purpose. The Hobo bag
- Replica Bell & Ross:2011-12-02 (Fri) 04:17
-
undoubtedly among the favorite type of informal shoulder bags. You can make a fashion statement this year if you are carrying one of these silver designer shoulder bags.Backpacks are
- louis vuitton outlet:2011-12-12 (Mon) 02:26
-
New style louis vuitton outlet are all come with our online store,
100% authentic and 100% Satisfaction 2011 louis vuitton bags are accumulation cheapest price.
High quality guarantee with amazing low price from our louis vuitton outlet store online,
Come and just enjoy shopping here, what you see will the same as what you get,
buy cheap louis vuitton online now!
- UGG ムートン ブーツ:2011-12-21 (Wed) 21:36
-
だけでなく UGG ムートン ブーツ,良いプロセスからだけでなく UGG ブーツ 販売,その特殊な素材で高級 UGG ブーツ 激安,オーストラリアと米 UGG クラシックミニ,UGG ブーツ キッズ,ジミーチュウ 靴,UGG ベイリーボタン コーディネート,UGG クラシックショート コーディネート,UGG ムートンバッグ,UGG クラシックトール ブーツ
- nike free:2012-01-03 (Tue) 21:07
-
We know that people who love nike free barefoot always want the newest and hottest styles on the market, Our customers are usually amazed that products that they can't find anywhere else are available on our website, and all shoes are authentic.
- birkin bags :2012-01-16 (Mon) 04:55
-
THE remembrance. Global set limit to 1971 only.
Table back in addition to cast carved words and set limit to of the most impressive, the Numbers of the lunar way car totem, extremely has the collection value. - lv taiga:2012-01-16 (Mon) 04:56
-
Since 1988, senior wrist watch brand Chopard Chopin will spare no effort in the world of racing sponsorship classic event "Mille Miglia" antique sports car rally, and
- chanel bag:2012-01-16 (Mon) 04:57
-
watch money; More of a year out to the event design, salute.
Mille Miglia antique sports car rally origin in 1927, by four love racing figure composed; No matter your identity and status are can sign up to take, but limited to famous depot - louis vuitton wallet:2012-01-16 (Mon) 04:59
-
manufacturing, without modification of conventional vehicles can take part in the games. This event by the northern Italian Brescia, to start to return to Brescia, the fixtures after Rome about 1000 miles total, and this is the event later called "Mille Miglia" origin (Mille Miglia of Italian
Trackback:0
- TrackBack URL for this entry
- http://blogs.grf-design.com/mt/mt-tb.cgi/224
- Listed below are links to weblogs that reference
- routing.yml について: 基本と応用 from The Croton