超シンプルで高速なPHPルーター
Composerを使用してMoxRouterをインストールすることをお勧めします。
$ composer require moxxie/moxrouter " ^0.2.0 " 次の内容を持つindex.phpファイルを作成します。
<?php
require ' vendor/autoload.php ' ;
$ router = new Moxxie MoxRouter ();
$ router -> get ( ' /{message} ' , function ( $ message ){
echo " Hello " . $ message . " ! " ;
});
$ router -> run ();これは、PHPに付属の組み込みサーバーを使用してテストできます。
$ php -S localhost:8000http:// localhost:8000/worldが「hello、world!」を表示します。
moxRouterは、httpリクエストをget、post、put、patch、削除をサポートします
// Will only match GET HTTP requests
$ router -> get ( ' /product/{id} ' , function ( $ id ){
// Return product with id = $id
});
// Will only match POST HTTP requests
$ router -> post ( ' /product ' , function (){
// Create new a product
});
// Will only match PUT HTTP requests
$ router -> put ( ' /product/{id} ' , function ( $ id ){
// Update product with id = $id
});
// Will only match PATCH HTTP requests
$ router -> patch ( ' /product/{id} ' , function ( $ id ){
// Apply changes made to product with id = $id
});
// Will only match DELETE HTTP requests
$ router -> delete ( ' /product/{id} ' , function ( $ id ){
// Delete product with id = $id
});
<?php
$ router = new Moxxie MoxRouter ();
// Create an empty container
$ container = [];
// Add a service to the container
$ container [ ' service ' ] = function (){
return new Service ();
};
$ router -> get ( ' / ' , function (){
// Use the new Service
$ service = $ this -> service ();
});
// Run the router with the container
$ router -> run ( $ container );ルートの前
$ router -> before ( function (){
// This code will be executed before a route has been executed
});ルートの後
$ router -> after ( function (){
// This code will be executed after a route has been executed
});デフォルトの404ハンドラーをオーバーライドできます
$ router -> notFound ( function (){
// This code will be executed when a route is not found
});apache .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]nginx
try_files $uri /index.php;(MITライセンス)
Copyright(c)2017 Moxxie -https://github.com/moxxie/moxrouter
このソフトウェアと関連するドキュメントファイル(「ソフトウェア」)のコピーを入手して、制限なしにソフトウェアを扱うために、このソフトウェアを制限する権利を含め、ソフトウェアのコピーをコピー、変更、公開、配布、販売する、ソフトウェアのコピーを許可する人を許可する人を許可することを含めて、許可が無料で許可されます。
上記の著作権通知とこの許可通知は、ソフトウェアのすべてのコピーまたはかなりの部分に含まれるものとします。
このソフトウェアは、商品性、特定の目的への適合性、および非侵害の保証を含むがこれらに限定されない、明示的または黙示的なものを保証することなく、「現状のまま」提供されます。いかなる場合でも、著者または著作権所有者は、契約、不法行為、またはその他の訴訟、ソフトウェアまたはソフトウェアの使用またはその他の取引に関連する、またはその他の契約、またはその他の請求、またはその他の責任について責任を負いません。