mamphir/extreemly-simple-router 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mamphir/extreemly-simple-router

Composer 安装命令:

composer require mamphir/extreemly-simple-router

包简介

Very simple PHP router that can easly be integrated on your app and easly modified to fit your needs

README 文档

README

This is an PHP class that can help to add router on your PHP project. Main ideas is to create PHP router as simple as it gets, that can be integrated in your project and easy to be edited for your needs.

Usage

Import router.php in your project and create an instance of Router class. Add route to the instance using addRoute method by providing:

  • Route : define a route to match
  • Method : method of request to apply for
  • Handler : handler function that accepts array of arguments, this arry will be provided as key value pair when route matches
//import Router class
require_once("./router.php");

//create instance of Router
$router = new \ESRouter\Router();

//define new route
$router->addRoute("/user/{name}/post/{id:[0-9]}", "get", function ($args) {
    //handle request as you need
    $name = $args['name'];
    $id = $args['id'];
    print $name.$id ;
});

//define another route
$router->addRoute("/", "get", function ($args) {
    //handle request as you need
    print "Welcome home" ;
});

//more routes
//...

//optional
//Setting 404 handler
$router->setE404(function(){
    echo "this is 404";
});


$router->run();

Defining Routes

To define route, start with / and for static rout use plain text example user or for variable use {name} ad it fill match anything in between / and / or end of route, in case you want to use RegEx use {varname:RegEx} where name is the name of variable and RegEx is a RegEx expression to match

Matching Routes

Routes are matchef rom the first defined route to the last, if a route is matched than it will execute the handler and will stop searching for other routes.

//Example
//In this case if we call http://<server>/user/nameOfTheUser/post/1234

$router->addRoute("/user/{name}/post/{id:\d{4}}", "get", function ($args) {
    print 'First route';
});

$router->addRoute("/user/{name}/post/{id:[0-9]}", "get", function ($args) {
    print 'Second route';
});

//First Route will be called

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固