독립적이고 사용하기 쉽고 확장 가능하며 이해하기 쉬운 PHP 웹 응용 프로그램 URL 라우팅 라이브러리 인 Mybe는 개발 툴킷을 강화하거나 약간의 영감을 제공했습니다.
소스 코드를 다운로드하고 프로젝트 디렉토리의 올바른 위치를 배치하십시오.
라우터 개체 인스턴스를 가져옵니다
use RouterOne Router ;
$ router = Router:: getInstance (); 단일 파일에서 경로 맵을 만들고 prject의 특정 dir에 위치하십시오. $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 ' ]);
Route Map 파일 디렉토리 경로를 설정하고로드하십시오. (경로지도 파일 기본 확장은 .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
}); Middleware는 RouteMiddleWareInterface 를 구현해야합니다. MiddleWare Dir와 같은 임의의 디렉토리에서 Middle-Ware 클래스 파일을 찾을 수 있습니다.
일반적인 중간웨어 클래스에는 다음과 같이 경로 조치- $action 매개 변수가있는 handle() 메소드가 포함되어 있습니다.
use RouterOne MiddleWare RouteMiddleWareInterface ;
class AuthCheckMiddleWare implements RouteMiddleWareInterface
{
public static function handle ( $ action )
{
if ( ! AdminUser::Logged) {
exit ( ' Please login first. ' );
}
$ action ();
}
} 어떤 경우에는 경로 조치가 낙제 된 후 일부 프로세스를 원할 수도 있습니다. $action() 호출 문 뒤에 중간 퇴적 논리를 배치하십시오.
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() 는 두 개의 매개 변수를 가지고 있으며, 첫 번째는 middle-ware 클래스 이름 배열이며 여기서는 더 많은 중간 전쟁을 지원하며 다른 하나는 클로저 기능이 공통 경로 매핑을 포함합니다.
$ 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 경로이기도하며 특정 경로에 실용적인 접두사와 접미사를 편리하게 추가 할 수 있습니다.
Prefix 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() 를 사용하여 접미사 .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 ' , ...);
. . .
});