忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。


share

2025/04/09 19:20 |
PECL uri_templateでRFC6570を使う
RFC6570というRFCがあります。
http://tools.ietf.org/html/rfc6570

簡単に言うと、
http://example.com/{controller}/{action}』を『http://example.com/hoge/fuga』に展開できたら楽じゃね、という最近のフレームワークでよく見かけるルーティングをRFCに仕上げたものです。

フレームワークならだいたい最初から付属しているし、自力で作るにしても簡単なのならSmartyなりZend\Uri\Uriなりで十分ですが、RFC6570に沿ったエクステンションがあったので試してみます。
使用したバージョンは1.0です。
001<?php
002     
003    // 変換テーブル
004    $table = [
005        'var'   => 'value',
006        'hello' => 'Hello World!',
007        'path'  => '/foo/bar',
008        'empty' => '',
009        'x'     => '1024',
010        'y'     => '768',
011        'list'  => ['red', 'green', 'blue'],
012        'keys'  => ['semi'=>';','dot'=>'.','comma'=>','],
013    ];
014     
015    // レベル1
016    $level1 = [
017        '{var}' => 'value',
018        '{hello}' => 'Hello%20World%21',
019    ];
020    // レベル2
021    $level2 = [
022        '{+var}' => 'value',
023        '{+hello}' => 'Hello%20World!',
024        '{+path}/here' => '/foo/bar/here',
025        'here?ref={+path}'=>'here?ref=/foo/bar',
026        'X{#var}'=>'X#value',
027        'X{#hello}'=>'X#Hello%20World!',
028    ];
029    // レベル3
030    $level3 = [
031        'map?{x,y}' => 'map?1024,768',
032        '{x,hello,y}' => '1024,Hello%20World%21,768',
033        '{+x,hello,y}' => '1024,Hello%20World!,768',
034        '{+path,x}/here' => '/foo/bar,1024/here',
035        '{#x,hello,y}' => '#1024,Hello%20World!,768',
036        '{#path,x}/here' => '#/foo/bar,1024/here',
037        'X{.var}' => 'X.value',
038        'X{.x,y}' => 'X.1024.768',
039        '{/var}' => '/value',
040        '{/var,x}/here' => '/value/1024/here',
041        '{;x,y}' => ';x=1024;y=768',
042        '{;x,y,empty}' => ';x=1024;y=768;empty',
043        '{?x,y}' => '?x=1024&y=768',
044        '{?x,y,empty}' => '?x=1024&y=768&empty=',
045        '?fixed=yes{&x}' => '?fixed=yes&x=1024',
046        '{&x,y,empty}' => '&x=1024&y=768&empty=',
047    ];
048    // レベル4
049    $level4 = [
050        '{var:3}' => 'val',
051        '{var:30}' => 'value',
052        '{list}' => 'red,green,blue',
053        '{list*}' => 'red,green,blue',
054        '{keys}' => 'semi,%3B,dot,.,comma,%2C',
055        '{keys*}' => 'semi=%3B,dot=.,comma=%2C',
056        '{+path:6}/here' => '/foo/b/here',
057        '{+list}' => 'red,green,blue',
058        '{+list*}' => 'red,green,blue',
059        '{+keys}' => 'semi,;,dot,.,comma,,',
060        '{+keys*}' => 'semi=;,dot=.,comma=,',
061        '{#path:6}/here' => '#/foo/b/here',
062        '{#list}' => '#red,green,blue',
063        '{#list*}' => '#red,green,blue',
064        '{#keys}' => '#semi,;,dot,.,comma,,',
065        '{#keys*}' => '#semi=;,dot=.,comma=,',
066        'X{.var:3}' => 'X.val',
067        'X{.list}' => 'X.red,green,blue',
068        'X{.list*}' => 'X.red.green.blue',
069        'X{.keys}' => 'X.semi,%3B,dot,.,comma,%2C',
070        'X{.keys*}' => 'X.semi=%3B.dot=..comma=%2C',
071        '{/var:1,var}' => '/v/value',
072        '{/list}' => '/red,green,blue',
073        '{/list*}' => '/red/green/blue',
074        '{/list*,path:4}' => '/red/green/blue/%2Ffoo',
075        '{/keys}' => '/semi,%3B,dot,.,comma,%2C',
076        '{/keys*}' => '/semi=%3B/dot=./comma=%2C',
077        '{;hello:5}' => ';hello=Hello',
078        '{;list}' => ';list=red,green,blue',
079        '{;list*}' => ';list=red;list=green;list=blue',
080        '{;keys}' => ';keys=semi,%3B,dot,.,comma,%2C',
081        '{;keys*}' => ';semi=%3B;dot=.;comma=%2C',
082        '{?var:3}' => '?var=val',
083        '{?list}' => '?list=red,green,blue',
084        '{?list*}' => '?list=red&list=green&list=blue',
085        '{?keys}' => '?keys=semi,%3B,dot,.,comma,%2C',
086        '{?keys*}' => '?semi=%3B&dot=.&comma=%2C',
087        '{&var:3}' => '&var=val',
088        '{&list}' => '&list=red,green,blue',
089        '{&list*}' => '&list=red&list=green&list=blue',
090        '{&keys}' => '&keys=semi,%3B,dot,.,comma,%2C',
091        '{&keys*}' => '&semi=%3B&dot=.&comma=%2C',
092    ];
093     
094    // 確認
095    $level = array_merge($level1, $level2, $level3, $level4);
096    foreach($level as $key=>$val){
097        // 変換
098        $template = uri_template($key, $table);
099        if($template !== $val){
100            print('えらー');
101        }
102    }
エラーは出ません。
RFC6570には4段階のレベルがありますが、uri_templateは全レベル対応しています。
完全にRFC6570に従ったURIを出力しました。
これでURIを組み立てるのが便利に…なるのか?
普通にrawurlencodeとか使った方が早い気がしないでもない。
PR

share

2014/07/11 22:42 | Comments(0) | PHP

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<買ったものリスト 2014/07/13 | HOME | Windows7でVagrantの環境整備続き>>
忍者ブログ[PR]