承接 ooobii/quick-router 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ooobii/quick-router

Composer 安装命令:

composer require ooobii/quick-router

包简介

A quick & easy library for routing incoming HTTP requests with PHP.

README 文档

README

Build Status Test Status Composer

A quick & easy way to start developing an API / web service application using PHP & Apache.

Heads Up! Documentation is not complete. There may be some gaps in usage instructions.

Requirements

  • PHP (7.3 or later)
  • Apache
    • Rewrite extension enabled
    • .htaccess files enabled

Installation

This package can be installed with composer:

composer require ooobii/quick-router

In order to use this package, an .htaccess file / rewrite rules are required to defined so that all requests are processed by the index.php file. You can create / update one in the site's directory by executing the following PHP script:

<?php
require_once __DIR__ . '/vendor/autoload.php';

ooobii\QuickRouter\Helpers\SetupHtaccess::createHtaccessFile();

This method will create a .htaccess file in your project's root folder. If one already exists, the rewrite rules that this package requires will automatically be appended to the end of it. The rules this method generates can also be defined in Apache's virtual host configuration if you'd prefer to have them there.

Usage

Create a Router

You can define a new router by instantiating a new Router class:

use \ooobii\QuickRouter\Router\Router;

$router = new Router('/api/v1', TRUE);

if(!$router->process()) {
    //url provided doesn't have a route specified.
}

You can pass 2 arguments to the Router class constructor:

  • "Router Root":

    • Any URL requests that begin with the root provided will be handled by the router.
    • If the URL being requested is outside the scope of this root, the router will ignore the route.

    For example, with the router above, the URL http://<SITE>/v1/someEndpoint would not be handled by this router, but http://<SITE>/api/v1/someEnpoint would be so long as a route is defined for it.

  • "Enforce JSON Output":

    • false (default): allow's the handlers for each route to have full control of the output content.
    • true: all route handlers will attempt to be converted to JSON by passing the results of the handler through json_encode().

You can also define multiple routers with different root URLs:

use \ooobii\QuickRouter\Router\Router;

$router_v1 = new Router('/api/v1', TRUE);
$router_v2 = new Router('/api/v1', TRUE);

if(!$router_v1->process() && !$router_v2->process()) {
    //url provided doesn't have a route specified.
}

You can pass these routers by reference to an external script file to setup routes for each router. Any routers that are defined must be included in the index.php file.

Define Routes in a Router

Error Handle Routes

Once you instantiate a router, you can add handlers for errors that are encountered by specific HTTP error codes:

//define an error route for exceptions encountered during route handling.
$router->addErrorRoute(500, function($exception) {
    return [ 'error' => $exception->getMessage() ];
});

//define an error route for requests that do not have endpoints defined.
$router->addErrorRoute(404, function() {
    return [ 'error' => 'Endpoint not found.' ];
});

The methods above add return handlers for specific HTTP error codes. The first argument is the HTTP error code, and the second argument is a callback function that will be executed when the route is matched. The callback function will receive the exception object as its only argument.

Note: In this example, it is assumed that JSON output enforcement is enabled. Be sure your route handlers returns a proper echo type or echo's out a proper result based on the content being delivered.

Define Standard Routes

ooobii/quick-router 适用场景与选型建议

ooobii/quick-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ooobii/quick-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ooobii/quick-router 我们能提供哪些服务?
定制开发 / 二次开发

基于 ooobii/quick-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-27