定制 aniruddh/phprouter 二次开发

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

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

aniruddh/phprouter

最新稳定版本:v1.0.0

Composer 安装命令:

composer require aniruddh/phprouter

包简介

A simple, lightweight, and fast PHP router with supported middleware and trailing slash or non-trailing slash mode.

README 文档

README

Install

To install with composer:

composer require aniruddh/phprouter

Requires PHP 7.4 or newer.

Usage

Here's a basic usage example:

require_once('path/to/vendor/autoload.php');
use Aniruddh\Router\Router;
$router = new Router();
// default method is GET does not required to specify
$router->add('/', function() {
    echo 'get Home page';
});

$router->add('/test', function() {
    echo 'get test1';
});

$router->add('/test', function() {
    echo 'post test';
});

//you can specify if you want
$router->add('/test1', function() {
    echo 'get test1';
    
}, ['GET']);

//Except GET you have to specify the method
$router->add('/test1', function() {
    echo 'post test1';
}, ['POST']);

//you can specify multiple method
$router->add('/test2', function() {
    echo 'get and post test2';
}, ['GET', 'POST']);

//set 404 if not default 404 page shown
$router->set404(function(){
    echo "<h1>404 not found! </h1> url: " . htmlentities($_SERVER['REQUEST_URI']);
});
//run the router
$router->run();

Defining routes

//method and middleware must be an array
$route->add($route, $callback, $method, $middleware);
//router use regex for route matching
// Matches /users/42, but not /users/abc
$router->add('/users/\d+', 'callback');
// Example with middleware
$route->add('/users/\d+', 'callback', ['GET'], ['before'=> callabck, 'after'=> callback]);
//Example with class object and method
$router->('/users', ['classname','method']);
//Example with trailing slash option enable
require_once('path/to/vendor/autoload.php');

use Aniruddh\Router\Router;

$router = new Router(true, 1); //default is false and 0 // 0 means without trailing slash
// default method is GET does not required to specify
$router->add('/', function() {
    echo 'get Home page';
});
$router->('/users', callback); //use can specify route like $router->('users', callback); both version will work

Note

coming soon...

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-17