ruta
1.0.0

PHP 용 가벼운 HTTP 라우팅 라이브러리. (WIP)
multipart/form-data 요청 데이터를 처리합니다 x-www-form-urlencoded 요청 데이터를 처리하십시오 application/xml 요청 데이터를 처리합니다 application/json 요청 데이터를 처리합니다 query URI 요청 매개 변수를 처리하십시오 path 및 headers /abc/{some} /abc/regex(id=^[0-9]+$) /abc/{some?} Ruta를 기반으로 한 Leap Micro-Framework를 참조하십시오.
PHP 8.0 이상.
작곡가를 통해 설치하십시오
composer require joseluisq/ruta:dev-master <?php
declare (strict_types= 1 );
require ' vendor/autoload.php ' ;
use Ruta Header ;
use Ruta Method ;
use Ruta Ruta ;
use Ruta Request ;
use Ruta Response ;
use Ruta Status ;
// 1. Callback style
// NOTE: `Request`, `Response`, `array` (slug arguments) are passed to the callback.
// However they are optional and their order can be modified. See more examples below.
Ruta:: get ( ' /home/hola ' , function ( Request $ req , Response $ res ) {
$ res -> json ([
' host ' => $ req -> header (Header::Host),
' headers ' => $ req -> headers (),
]);
});
Ruta:: get ( ' /home/hola/redirect ' , function ( Response $ res ) {
$ res -> redirect ( ' /home/aaa/some/bbb ' );
});
Ruta:: get ( ' /reg/regex(id=^[0-9]+$)/exp ' , function ( Response $ res , array $ args ) {
$ res -> json ([ ' args ' => $ args ]);
});
Ruta:: post ( ' /home/{path3}/some2 ' , function ( Response $ res ) {
$ res -> json ([ ' post_data ' => 11010101010 ]);
});
Ruta:: some ( ' /home/some ' , [Method:: POST , Method:: PUT ], function ( Request $ req , Response $ res ) {
$ res -> json ([ ' only ' => $ req -> method ()]);
});
Ruta:: any ( ' /home/methods ' , function ( Request $ req , Response $ res ) {
$ res -> json ([ ' method ' => $ req -> method ()]);
});
Ruta:: post ( ' /home/{path} ' , function ( Response $ res ) {
$ res
-> header ( ' X-Header-One ' , ' Header Value 1 ' )
-> header ( ' X-Header-Two ' , ' Header Value 2 ' )
-> json ([ ' some_data ' => 223424234 ]);
});
// 2. class/method style
class HomeCtrl
{
public function index ( Request $ req , Response $ res , array $ args )
{
// 2.1 $args contains route placeholder values
if ( array_key_exists ( ' path1 ' , $ args )) {
// do something...
}
// 2.2. Get data provided via `multipart/form-data`
$ data = $ req -> multipart ();
// 2.3. Get all headers
$ data = $ req -> headers ();
// 2.4. Get a single header
$ data = $ req -> header ( " Host " );
// 2.5. Get data provided via `application/x-www-form-urlencoded`
$ data = $ req -> urlencoded ();
// 2.6. Get data provided via `application/json`
$ data = $ req -> json ();
// 2.7. Get data provided via `application/xml`
$ data = $ req -> xml ();
// 2.8. Get query data
$ data = $ req -> query ();
$ res -> json ([ ' data ' => ' Message from a class! ' ]);
}
// Custom 404 reply
public function not_found ( Response $ res )
{
$ res
-> status (Status::NotFound)
-> text ( " 404 - Page Not Found! " );
}
}
Ruta:: get ( ' /home/{path1}/some/{path2} ' , [HomeCtrl::class, ' index ' ]);
// 3. Handle 404 not found routes
Ruta:: not_found ([HomeCtrl::class, ' not_found ' ]);파일 : 예제/nginx/public/index.php
# Or run example using Docker + Nginx server
make compose-up # Run example using the PHP built-in server
make container-dev이제 http : // localhost : 8088/home/hola로 이동하십시오
풀 요청을 보내거나 일부 문제를 제출하십시오.
귀하가 명시 적으로 명시 적으로 명시하지 않는 한, APACHE-2.0 라이센스에 정의 된대로 귀하가 현재 작업에 포함시키기 위해 의도적으로 제출 된 모든 기부금은 추가 약관이나 조건없이 아래에 설명 된대로 이중 라이센스가 있어야합니다.
풀 요청 또는 문제를 자유롭게 보내십시오.
이 작업은 주로 MIT 라이센스와 Apache 라이센스 (버전 2.0)의 조건에 따라 배포됩니다.
© 2021 제시 Jose Quintana