定制 badluck/badrouter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

badluck/badrouter

Composer 安装命令:

composer require badluck/badrouter

包简介

A simple Sinatra inspired router class for PHP

README 文档

README

A minimalist router inspired by Ruby's Sinatra router.

Visit the official documentation page here: https://badrouter.badluck.io

Install

1. Run composer require badluck/badrouter in your project directory to install the package and its dependencies.

2. Require the autoloader. At the beginning of your PHP file, require the Composer autoloader by adding this line of code:

require_once('./vendor/autoload.php');

3. Use the Router static class.

You can use ::get, ::post, ::put, and ::delete. BadRouter supports dynamic routes in {braces}.

Router::get('/user/{id}', function ($id) {
  $locals = [
    "user_id" => $id
  ];
  Router::render('/user', $locals);
});

4. Configure the view and public directories.

The public directory is where the router will search for static files if the URI is not a route. The views directry is where Router::render will search when rendering pages.

Router::set_public('public');
Router::set_views('views');

5. After the router is configured, run it.

Router::run();

Full Usage example

<?php
require_once('./vendor/autoload.php');

use BadRouter\Router;

Router::use(function() {
  // Middleware
});

// Setup routes
Router::get('/', function() {
  $locals = [
    'message' => 'Hello world!'
  ];

  Router::render('/home', $locals);
});

Router::get('/login', function() {
  Router::render('/login', [], null);
});

Router::get('/about', function() {
  $locals = [
    'message' => 'We are awesome!',
  ];

  Router::render('/about', $locals);
});

Router::get('/message', function() {
  Router::set_content_type('json');
  Router::json(['message' => 'About Us']);
});

Router::get('/redirectMe', function() {
  Router::redirect('/about');
});

Router::get('/user/{id}', function ($id) {
  $locals = [
    "id" => $id
  ];
  Router::render('/user', $locals);
});

// Restricted /admin route
function restricted() {
  if (!isset($_SESSION['user'])) {
    Router::redirect('/login');
    return false;
  }
}

Router::get('/admin', function() {
  restricted();
  Router::render('/admin/page');
});

// POST
Router::post('/api/login', function() {
  Router::json([
    'success' => true,
  ]);
});

// Set 404 page
Router::set_error(404, function() {
  $locals = [
    'route' => parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH),
  ];
  Router::render('/404', $locals, null);
});

// Configure directories
Router::set_public('public');
Router::set_views('views');

// Run
Router::run();

Change Log

Log types:

  • Added
  • Changed
  • Fixed
  • Moved
  • Removed
  • Renamed
  • Updated

v0.4.5 (2023-07-26)

  • Added styling rules

v0.4.4 (2023-05-03)

  • Fixed bug with fetching views using relative directory paths

v0.4.3 (2023-05-03)

  • Fixed incorrect Content-Type for 404

v0.4.2 (2023-05-03)

  • Fixed bug in URI parsing

v0.4.1 (2023-05-02)

  • Fixed file search path missing a slash

v0.4.0 (2023-05-01)

  • Added 74 MIME types
  • Changed static files automatically fetched and no longer need PUBLIC_PATH to be specified

v0.3.4 (2023-04-26)

  • Removed Router::json datatype for input

v0.3.3 (2023-04-24)

  • Fixed warning $server['HTTP_REFERER'] with null coalescence

v0.3.2 (2023-04-18)

  • Fixed Composer PHP requirements from "^7.4.33" to ">=7.4.33"

v0.3.1 (2023-04-17)

  • Fixed route not checking against the Request object
  • Fixed Router::render $layout param not being nullable

v0.3.0 (2023-04-17)

  • Added class Request
  • Added variable type specifications to method params
  • Added variable type to method returns
  • Updated middleware use to specify Closure(Request) type
  • Updated documentation to include change logs

v0.2.5 (2023-04-13)

v0.2.4 (2023-04-13)

  • Fixed (another) bug in Router::set_base_path
  • Moved example website from this package and into its own repo at https://github.com/BadLuckSoftware/BadRouter-Example
  • Updated $request fields to be null if $_SERVER field was null
  • Updated Router::redirect to use BASE_PATH instead of self::$base_path

v0.2.3 (2023-04-13)

v0.2.2 (2023-04-13)

  • Added $request parameter to middleware
  • Added VIEWS_PATH global variable
  • Added Router::json method
  • Renamed directory example_website to example
  • Updated example website to reflect the current features of BadRouter
  • Updated Router::set_base_path to default empty paths to '/'

v0.2.1 (meow)

  • The cow goes moo

v0.2.0 (2023-04-11)

  • Added BASE_PATH global variable
  • Added Router::set_base_path method
  • Added Composer keywords
  • Changed PUBLIC_DIR global variable to PUBLIC_PATH
  • Moved BadRouter repo to BadLuckSoftware under badluck namespace
  • Updated Composer description

v0.1.3 (2023-04-06)

  • Updated documentation

v0.1.2 (2023-04-05)

  • Renamed file BadRouter.php to Router.php
  • Updated example in readme
  • Updated example website to use the correct Router name

v0.1.1 (2023-04-05)

  • Added forgotten namespace
  • Renamed class BadRouter to Router

v0.1.0 (2023-04-05)

  • Added initial BadRouter Composer package

badluck/badrouter 适用场景与选型建议

badluck/badrouter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 222 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 04 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「routing」 「router」 「middleware」 「sinatra」 「website」 「badluck」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 badluck/badrouter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-11