PHP WebアプリケーションのURLルーティングライブラリを理解しやすく、使いやすく、拡張性があり、簡単に理解できるようになりました。Mybeは、開発ツールキットを拡張するか、少しインスピレーションを与えました。
ソースコードをダウンロードし、プロジェクトディレクトリの適切な場所を配置します。
ルーターオブジェクトインスタンスを取得します
use RouterOne Router ;
$ router = Router:: getInstance ();単一のファイルでルートマップを作成し、Prjectの特定の監督に配置され、ファイル$this concrete $routerオブジェクトインスタンスを参照してください。 HTTP要求の動詞の場合、 RouteOneのみサポートGET 、 POST (PHPの$ _GET&$ _POSTに合わせて、必要に応じて完全に拡張します。)
たとえば、 Foo.php 。 Routeroneは、 frontend_route_map.php 、 backend_route_map.phpなど、複数のルートマップファイルをサポートできます。
// Closure function call
$ this -> get ( ' / ' , function () {
echo ' Hello! RouterOne ' ;
});
// `WelcomeController::hello()` call
$ this -> get ( ' / ' , [WeclomeController::class, ' hello ' ]);
ルートマップファイルディレクトリパスを設定し、ロードします。 (ルートマップファイルのデフォルト拡張機能は.phpです)
$ router -> setIncludePath (` YOUR_ROUTE_MAP_FILE_DIR `);
$ router -> load ([ ' Foo ' ]); // Just file's name without extensionまたは、このように電話してください
$ router -> setIncludePath (` YOUR_ROUTE_MAP_FILE_DIR `)-> load ([ ' Foo ' ]); // Just file's name without extension発送を実行し、すべてのロードされたルートを有効にすると、実際に呼ばれるルートに関する結果が返されます。
$ res = $ router -> dispatch ();コントローラールートアクション:
// Http GET
$ this -> get ( ' index ' , [ Controllers SiteController::class, ' index ' ]);
// Http POST
$ this -> post ( ' news/add ' , [ Controllers NewsController::class, ' add ' ]);閉鎖ルートアクション:
// Http GET
$ this -> get ( ' index ' , function () {
/**
* Some logic process code here
*/
});
// Http POST
$ this -> post ( ' news/add ' , function () {
/**
* Some logic process code here
*/
});ルートパラメーター、 {}ラップを使用した動的パラメーターを使用すると、外観の順序によりコントローラーメソッドまたはClousre関数パラメーターに転送されます。
$ this -> get ( ' test/{param1}/{param2} ' , [ Controllers TestController::class, ' params ' ]);
class TestController
{
public function params ( $ param1 , $ param2 )
{
// Some code ...
}
} $ this -> get ( ' test/{param1}/{param2} ' , function ( $ param1 , $ param2 ) {
// Some code
});ミドルウェアは、 RouteMiddleWareInterfaceへの実装である必要があります。 MiddleWareディレクトリなどのミドルウェアクラスファイルを任意のディレクトリに見つけることができます。
典型的なミドルウェアクラスには、以下のように、ルートアクション - $actionパラメーターを備えたhandle()メソッドが含まれています。
use RouterOne MiddleWare RouteMiddleWareInterface ;
class AuthCheckMiddleWare implements RouteMiddleWareInterface
{
public static function handle ( $ action )
{
if ( ! AdminUser::Logged) {
exit ( ' Please login first. ' );
}
$ action ();
}
}場合によっては、ルートアクションがエクセートされた後に何らかのプロセスを実行する必要があります。 $action() callステートメントの後ろに中間wareロジックを配置するだけです。
use RouterOne MiddleWare RouteMiddleWareInterface ;
class AfterMiddleWare implements RouteMiddleWareInterface
{
public static function handle ( $ action )
{
$ action ();
echo ' This text will print after route action excuted. ' ;
}
}ミドルウェアを定義し、ルーターのmiddleware()メソッドを介してgrouped形式としてルートを設定できる場合、 middleware()には2つのパラメーターがあり、1つ目はmiddle-wareクラス名配列であり、ここではより多くのミドルウェアをサポートし、もう1つは閉鎖関数に共通のルートマッピングが含まれます。
$ this -> middleware (
[
AuthCheckMiddleWare::class,
...
. . .
], function () {
$ this -> get ( ' admin/index ' , [ Controllers Admin AdminController::class, ' index ' ]);
$ this -> get ( ' admin/news/list ' , [ Controllers Admin NewsController::class, ' list ' ]);
. . .
. . .
}); nestedこともできます
$ this ->middleware(
[
OuterMiddleWare::class,
], function () {
. . .
$ this ->middleware([
InnerMiddleWare::class
], function () {
$ this ->get(...
$ this ->post(...
. . .
});
...
. . .
});prefix()およびsuffix()メソッドはgroupedルートでもあり、特定のルートに実用的なプレフィックスとサフィックスを便利に追加できます。
プレフィックスstatic/ 、urls ':// domain/static/page1'、 '、' // domain/static/page2 'を追加します。
$ this -> prefix ( ' static/ ' , function () {
$ this -> get ( ' page1 ' , ...);
$ this -> get ( ' page2 ' , ...);
. . .
}); preifxを追加し、 the ':// domain/thepage1'、 '、' // domain/thepage2 'が一致します。
$ this -> prefix ( ' the ' , function () {
$ this -> get ( ' page1 ' , ...);
$ this -> get ( ' page2 ' , ...);
. . .
}); prefix()を使用して同じように、suffix .htmlを追加し、urlの変更を '://domain/page1.html'に変更します。
$ this -> suffix ( ' .html ' , function () {
$ this -> get ( ' page1 ' , ...);
. . .
. . .
}); prefix()とsuffix()の間で互いにnestedできます。
$ this -> prefix ( ' static/ ' , function () {
$ this -> get ( ' page1 ' , ...); // request url '://domain/static/page1' matched here
. . .
$ this -> suffix ( ' .html ' , function () {
$ this -> get ( ' page2 ' , ...); // request url '://domain/static/page2.html' matched here
});
});アプリケーションに複数のURLドメインがある場合、 domain()メソッドを使用すると、これらのドメインを区別し、リクエストを対応するルートグループに導くことができます。
$ this -> domain ( ' www.hereisyoursite.com ' , function () { // PC Pages
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' m.hereisyoursite.com ' , function () { // Mobile Pages
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' new.hereisyoursite.com ' , function () { // Current Domain routes
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' old.hereisyoursite.com ' , function () { // Legacy Domain routes
$ this -> get ( ' index ' , ...);
. . .
});