np/router

简单的PHP路由器

0.0.4 2020-03-30 14:00 UTC

This package is auto-updated.

Last update: 2024-09-29 05:22:06 UTC


README

简单的PHP路由器

安装

composer require np/router
require "vendor/autoload.php";

使用

use np\router;

$route = new router;

$route->get('/',function(){
     	echo 'GET method';
});
$route->post('/',function(){
	echo 'POST method';
});
$route->put('/',function(){
	echo 'PUT method';
});
$route->patch('/',function(){
	echo 'PATCH method';
});
$route->delete('/',function(){
	echo 'DELETE method';
});
$route->option('/',function(){
	echo 'OPTION method';
});

路由器中的数据

$route->get('/post/{id}',function($data){
	echo 'This is post page and id = '.$data['id'];
});

返回视图

function loadView($view){
	echo file_get_contents($view);
}
$route->get('/',function(){
	loadView('./dir_name');
});

监听(运行)

$route->listen();

设置错误(404)时的回调

$route->setCallbackError(function(){
	loadView('./404.php');
});

运行服务器

php -S localhost:8000
-> open localhost:8000