initphp/router

InitPHP HTTP 路由库

1.2.1 2023-03-20 12:32 UTC

This package is auto-updated.

Last update: 2024-09-20 15:40:35 UTC


README

这是一个开源库,允许您创建和管理 HTTP 请求的高级路由。

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

特性

  • 完全支持 GET、POST、PUT、DELETE、OPTIONS、PATCH、HEAD 和 ANY 请求方法。
  • 支持可变请求方法,带有(Laravel-like)$_REQUEST['_method']。默认是关闭的,也可以选择启用。
  • 支持控制器。例如:HomeController@about 或 HomeController::about
  • 支持中间件/过滤器(前和后)。
  • 支持静态和动态路由模式。
  • 可以创建自定义参数模式。
  • 支持命名空间。
  • 支持路由分组。
  • 支持基于域的路由。
  • 可以定义自定义 404 错误。
  • 可以给路由命名。
  • 可以在(Symfony-like)可调用函数或控制器方法参数中调用类。
  • 可以通过请求端口进行路由。
  • 可以通过客户端 IP 地址进行路由(通过 $_SERVER['REMOTE_ADDR']。在本地,此值可以是 ::1127.0.0.1 等。)
  • 可以将目录路径定义为虚拟链接。

要求

  • PHP 7.2 或更高版本
  • Apache 应设置 AllowOverride All,并且 mod_rewrite 应开启。
  • 任何实现了 Psr-7 HTTP Message Interface 的库,以及为 Psr-7 编写的发射器。例如:InitPHP HTTP

安装

composer require initphp/router

是否 Apache .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

是否 NGINX;

server {
	listen 80;
	server_name myinitphpdomain.dev;
	root /var/www/myInitPHPDomain/public;
	index index.php;
	location / {
		try_files $uri $uri/ /index.php?$query_string;
	}
	location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass unix:/var/run/php7.4-fpm.sock;
		fastcgi_index index.php;
		include fastcgi.conf;
		fastcgi_intercept_errors on;
	}
}

配置

$config = [
    'paths'                 => [
        'controller'            => null, //The full path to the directory where the Controller classes are kept.
        'middleware'            => null, //The full path to the directory where the Middleware classes are kept.
    ],
    'namespaces'            => [
        'controller'            => null, //Namespace prefix of Controller classes, if applicable.
        'middleware'            => null, //Namespace prefix of Middleware classes, if applicable.
    ],
    'base_path'             => '/', // If you are working in a subdirectory; identifies your working directory.
    'variable_method'       => false, // It makes the request method mutable with Laravel-like $_REQUEST['_method'].
    'argument_new_instance' => false, // This configuration is used for Request and Response objects that you want as arguments.
];

使用

以下示例使用 InitPHP HTTP 库。如果您愿意,可以使用以下命令使用此库,或者您可以使用使用 Psr-7 HTTP Message 接口的另一个库执行类似操作。

composer require initphp/http

请参阅 Wiki 以获取详细文档。

require_once "vendor/autoload.php";
use \InitPHP\HTTP\Message\{Request, Response, Stream};
use \InitPHP\HTTP\Emitter\Emitter;
use \InitPHP\Router\Router;

$request = Request::createFromGlobals();
$response = new Response();

// Create the router object.
$router = new Router($request, $response, []);

// ... Create routes.
$router->get('/', function () {
    return 'Hello World!';
});

$router->post('/login', function (Request $request, Response $response) {
    return $response->json([
        'status'        => 0,
        'message'       => 'Unauthorized',
    ], 401);
});

// If you do not make a definition for 404 errors; An exception is thrown if there is no match with the request.
$router->error_404(function () {
    echo 'Page Not Found';
});

// Resolve the current route and get the generated HTTP response object.
$response = $router->dispatch();

// Publish the HTTP response object.
$emitter = new Emitter;
$emitter->emit($response);

获取帮助

如果您有任何问题、疑虑、错误报告等,请在本存储库的问题跟踪器中提交问题。

参与进来

对本项目的所有贡献都将根据 MIT 许可证发布。通过提交拉取请求或提交错误、问题或功能请求,您同意遵守此版权放弃声明。

帮助的主要方式有两个

  • 使用问题跟踪器,
  • 更改代码库。

使用问题跟踪器

使用问题跟踪器提出功能请求、报告错误和提问。这也是与项目开发者和对此解决方案感兴趣的其他人建立联系的好方法。

使用问题跟踪器找到贡献的方法。找到一个错误或功能,在问题中说明您将承担这项工作,然后按照下面的“更改代码库”指南进行操作。

更改代码库

一般来说,您应该在此存储库上创建一个分支,在自己的分支中进行更改,然后提交一个拉取请求。所有新代码都应该有相关的单元测试来验证实现的功能和缺陷的存在或不存在。此外,代码应遵循项目规定的任何样式和架构指南。如果没有这样的指南,则模仿现有代码库中的样式和模式。

鸣谢

许可证

版权所有 © 2022 MIT 许可证