メインメニュー
XAMPP アレンジ
IED
WSL2
-
道具箱
リポジトリ編
フレームワーク編
公開ソフトウェア
メタ
リンク
- PHP ライブラリ
- PHP 言語
basic-library:fast-route:1.3文書の過去の版を表示しています。
投稿中です。しばらくお待ちください。
リクエストルーター - fast-route
Version 1.3 (BSD 3-Clause License)
— y2sunlight 2020-03-16
関連記事
- リクエストルーター - fast-route
リンク
- https://github.com/nikic/FastRoute — FastRouteの本家
テストプログラムの所在
{Project Folder}\test\fast-route\
fast-routeについて
TODO:インストール
composer require nikic/fast-route
Using version ^1.3 for nikic/fast-route ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing nikic/fast-route (v1.3.0): Downloading (100%) Writing lock file Generating autoload files 1 package you are using is looking for funding. Use the `composer fund` command to find out more!
Note:
Eclipse起動中にパッケージを取得した場合は、プロジェクト・エクスプローラー内の[プロジェクト(apricote)]を右クリックして[リフレッシュ]を選択して下さい。また、新しく取得したパッケージのインテリセンスが有効にならない場合は、プロジェクトのビルトまたはクリーン&ビルドを行ってビルドリストの更新を行って下さい。パッケージの取得が終わると
composer.json
のrequire
に以下が追加されます。- composer.json
{ "require": { "nikic/fast-route": "^1.3" } }
テストプログラム
TODO/test/fast-route/.htaccess
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] </IfModule>
- /test/fast-route/index.php
<?php require __DIR__.'/../../vendor/autoload.php'; // ルーティング(コールバック関数)の定義 // ルーティングは、get(path,handler),post(path,handler) の形式で指定する // (*1)本例では、handlerにcallable型を指定した // (*2)本例では、ベースとなるURLが同じなのでaddGroup()を使った) $base = '/ws2019/apricot/test/fast-route'; $routes = function (FastRoute\RouteCollector $r) use($base) { $r->addGroup($base, function (FastRoute\RouteCollector $r) use($base) { // handlerに関数を指定する $r->get('/users', 'users'); // 単純なルーティング $r->get('/user/{id:\d+}', 'user'); // 名前付きプレースフォルダ(名前:id) $r->get('/articles/{id:\d+}[/{title}]', 'articles'); // 任意指定のプレースフォルダ // handlerにクロージャを指定する $r->get('/index', function() use($base) { header("Location: " . $base.'/users'); }); }); }; $dispatcher = FastRoute\simpleDispatcher($routes); // dispatch()に与えるHTTPメソッドとURIを取得 // (URI末尾の/は削除してディレクトリアクセスも受け入れる) $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = rtrim($_SERVER['REQUEST_URI'],'/'); // dispatch()の妨げになるのでURIの中のクエリパラメータを除去する // (クエリパラメータは、通常に$_GETで取得できる) if (false !== ($pos = strpos($uri, '?'))) { $uri = substr($uri, 0, $pos); } // URIの中にクエリパラメータがあるかもしれないのでデコードする // Exp) /hoge/hoge/param/abc%20def $uri = rawurldecode($uri); // URIのディスパッチ $routeInfo = $dispatcher->dispatch($httpMethod, $uri); switch ($routeInfo[0]) { case FastRoute\Dispatcher::NOT_FOUND: // 404 Not Found break; case FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allowedMethods = $routeInfo[1]; // 許可されているメソッド // 405 Method Not Allowed break; case FastRoute\Dispatcher::FOUND: $handler = $routeInfo[1]; // ハンドラー 例)user $params = $routeInfo[2]; // パラメータハッシュ 例)['id'->123] // ハンドラーを呼び出す if (is_callable($handler)) { call_user_func_array($handler, $params); } break; } // Handler: users function users() { echo "users"; } // Handler: user function user(int $id) { echo "user({$id})"; } // Handler: articles function articles(int $id, string $title=null) { echo "articles({$id}){$title}"; }
basic-library/fast-route/1.3.1584861334.txt.gz · 最終更新: 2020/03/22 16:15 by y2sunlight
コメント