このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
basic-library:fast-route:1.3 [2020/03/22 14:49] y2sunlight [テストプログラム] |
basic-library:fast-route:1.3 [2020/07/14 15:36] y2sunlight [テストプログラム] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > 投稿中です。しばらくお待ちください。 | + | ====== リクエストルーター - FastRoute |
- | + | ||
- | ---- | + | |
- | + | ||
- | ====== リクエストルーター - fast-route | + | |
Version 1.3 ([[https:// | Version 1.3 ([[https:// | ||
行 11: | 行 7: | ||
関連記事 | 関連記事 | ||
- | * [[basic-library: | + | * [[basic-library: |
* [[basic-library: | * [[basic-library: | ||
* [[basic-library: | * [[basic-library: | ||
* [[basic-library: | * [[basic-library: | ||
* [[basic-library: | * [[basic-library: | ||
- | * リクエストルーター - fast-route | + | * リクエストルーター - FastRoute |
- | * [[basic-library: | + | * [[basic-library: |
+ | * [[basic-library: | ||
+ | * [[basic-library: | ||
* [[basic-library: | * [[basic-library: | ||
行 30: | 行 28: | ||
---- | ---- | ||
- | ===== fast-routeについて ===== | + | ===== FastRouteについて ===== |
- | >TODO: | + | FastRouteは、PHPコアの開発者の1人でもある [[https:// |
+ | |||
+ | リクエストリーターの選定に当たっては、最近の人気、実行速度、コードの品質から選びました。コードの品質については[[https:// | ||
+ | |||
+ | * FastRoute --- https:// | ||
+ | * Klein --- https:// | ||
+ | * AltoRouter --- https:// | ||
+ | |||
+ | 機能面を考えるとKleinとAltoRouterも捨てがたい魅力がありましたが、ORマッパーと同じく「兎に角シンプル」と「学習コストが少ない」そして何よりも'' | ||
===== インストール ===== | ===== インストール ===== | ||
行 58: | 行 64: | ||
パッケージの取得が終わると '' | パッケージの取得が終わると '' | ||
- | < | + | < |
{ | { | ||
" | " | ||
行 70: | 行 76: | ||
===== テストプログラム ===== | ===== テストプログラム ===== | ||
- | >TODO | + | テストフォルダ('' |
- | /test/fast-route/.htaccess | + | < |
- | <code> | + | |
< | < | ||
RewriteEngine On | RewriteEngine On | ||
行 82: | 行 87: | ||
</ | </ | ||
- | < | + | 以下にテストコード(' |
- | <?php | + | |
+ | 大まかな流れは「'' | ||
+ | |||
+ | <code php index.php> | ||
+ | <?php | ||
require __DIR__.'/ | require __DIR__.'/ | ||
- | $base_path | + | // 自分(index.php)の場所を取得する |
- | $routes = function(FastRoute\RouteCollector $r) use($base_path) | + | $base = dirname($_SERVER[' |
+ | if (preg_match('/^[\\.\\\\]$/', $base)) $base=' | ||
+ | |||
+ | // ルーティング(コールバック関数)の定義 | ||
+ | // ルーティングは、get(path, | ||
+ | // (*1)handlerにcallable型を指定した | ||
+ | // (*2)ベースとなるURLが同じなのでaddGroup()を使った) | ||
+ | $routes = function (FastRoute\RouteCollector $r) use($base) | ||
{ | { | ||
- | $r->addRoute(' | + | $r->addGroup($base, function (FastRoute\RouteCollector |
- | $r->addRoute(' | + | { |
- | $r->addRoute(' | + | // handlerに関数を指定する |
+ | $r->get('/ | ||
+ | $r->get('/ | ||
+ | $r->get('/ | ||
- | | + | |
- | $r->addRoute('GET', $base_path.' | + | $r->get('[/]', function() use($base) |
- | header(" | + | |
+ | | ||
+ | }); | ||
}); | }); | ||
}; | }; | ||
行 102: | 行 122: | ||
$dispatcher = FastRoute\simpleDispatcher($routes); | $dispatcher = FastRoute\simpleDispatcher($routes); | ||
- | // Fetch method and URI from somewhere | + | // dispatch()に与えるHTTPメソッドとURIを取得 |
$httpMethod = $_SERVER[' | $httpMethod = $_SERVER[' | ||
$uri = $_SERVER[' | $uri = $_SERVER[' | ||
- | // Strip query string | + | // dispatch()の妨げになるのでURIの中のクエリパラメータを除去する |
- | if (false !== $pos = strpos($uri, | + | // (クエリパラメータは、通常に$_GETで取得できる) |
- | | + | if (false !== ($pos = strpos($uri, |
+ | $uri = substr($uri, | ||
} | } | ||
+ | // URIの中にクエリパラメータがあるかもしれないのでデコードする | ||
+ | // 例) / | ||
$uri = rawurldecode($uri); | $uri = rawurldecode($uri); | ||
+ | // URIのディスパッチ | ||
$routeInfo = $dispatcher-> | $routeInfo = $dispatcher-> | ||
- | switch ($routeInfo[0]) { | + | switch ($routeInfo[0]) |
+ | { | ||
case FastRoute\Dispatcher:: | case FastRoute\Dispatcher:: | ||
- | // ... 404 Not Found | + | // 404 Not Found |
break; | break; | ||
+ | |||
case FastRoute\Dispatcher:: | case FastRoute\Dispatcher:: | ||
- | $allowedMethods = $routeInfo[1]; | + | $allowedMethods = $routeInfo[1]; |
- | // ... 405 Method Not Allowed | + | // 405 Method Not Allowed |
break; | break; | ||
+ | |||
case FastRoute\Dispatcher:: | case FastRoute\Dispatcher:: | ||
- | $handler = $routeInfo[1]; | + | $handler = $routeInfo[1]; |
- | $params = $routeInfo[2]; | + | $params = $routeInfo[2]; |
- | $handler($params); | + | |
+ | if (is_callable($handler)) | ||
+ | { | ||
+ | call_user_func_array($handler, | ||
+ | } | ||
break; | break; | ||
} | } | ||
- | // Action: users | + | // Handler: users |
- | function users($vars) | + | function users() |
{ | { | ||
echo " | echo " | ||
} | } | ||
- | // Action: user | + | // Handler: user |
- | function user($vars) | + | function user(int $id) |
{ | { | ||
- | echo " | + | echo " |
} | } | ||
- | // Action: articles | + | // Handler: articles |
- | function articles($vars) | + | function articles(int $id, string $title=null) |
{ | { | ||
- | | + | |
- | if(array_key_exists(' | + | |
- | | + | |
- | | + | |
- | | + | |
- | echo $response; | + | |
} | } | ||
</ | </ | ||
+ | |||
+ | \\ | ||
+ |