Windowsで作る - PHPプログラミングの開発環境
このページへのアクセス 今日: 5 / 昨日: 0 総計: 3615
文書の過去の版を表示しています。
投稿中です。しばらくお待ちください。
Version 1.3 (BSD 3-Clause License)
— y2sunlight 2020-03-16
定番ライブラリー に戻る
関連記事
リンク
テストプログラムの所在
{Project Folder}\test\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
{ "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>
<?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}"; }