定制 mark-veres/bedouin 二次开发

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

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

mark-veres/bedouin

最新稳定版本:1.0.1

Composer 安装命令:

composer require mark-veres/bedouin

包简介

A file-based PHP routing system.

README 文档

README

bedouin

A file-based PHP routing system.

Two files:

  • cartograph.php script that generates the map file
  • bedouin.php script that routes according to the map file

installation

composer require mark-veres/bedouin

example setup

# index.php
require_once "bedouin.php";

$router = new \Bedouin\Router;
$router->loadMap("map.json");
$route = $router->currentRoute();
if (isset($route->file)) include $route->file;
# gen_map.php
require_once "./cartograph.php";
$cart = new \Bedouin\Cartograph;
$cart->root_dir = __DIR__;
$cart->route_folders = ["./routes", "./api"];
$cart->printMap("map.json");

usage

  • copy the bedouin.php and cartograph.php files in the desired directory
  • create the routes and static directory
  • create an index.php file will handle all requests
  • redirect all requests to index.php
  • create another PHP file that will use the cartograph API to generate the map

directory structure

file url method
/routes/index.php / all
/routes/index.get.php / get
/routes/about.php /about all
/posts/index.php /posts all
/posts/[slug]/index.php /posts/test
/posts/bla-bla
all
/posts/[slug]/new.post.php /posts/test/new post

404 pages

  • create a 404.php file in the routes folder
  • this file does not support custom HTTP methods

static files

  • put all your static files in the static directory
  • access these files at the /static/* url
  • file names are case- and extension-sensitive

accessing route parameters

  • given a route /posts/[slug]
  • and an url /posts/test
$router = new \Bedouin\Router;
// ...
print_r($router->params);
/*
  Array ( [slug] => test )
*/

middleware

  • create a file ending in .mw.php or .mw.get.php (or any HTTP method for that matter)
  • the middleware will be "bound" to the index handler of the directory it is placed in

Note

coming soon:

  • map splits (performance optimization when dealing with many routes)
  • templates

Tip

Redirecting all requests to bedouin.php on an Apache server.

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

Tip

Redirecting all requests to bedouin.php with the PHP built-in server

php -S localhost:8080 bedouin.php

Tip

Regenerating the map by accessing a specific route.

# index.php
require_once "bedouin.php";

if ($_SERVER["REQUEST_URI"] == "/your/custom/path") {
  require_once "./cartograph.php";
  $cart = new \Bedouin\Cartograph;
  $cart->printMap("map.json");
}

$router = new \Bedouin\Router;
$router->loadMap("map.json");
$route = $router->currentRoute();
if (isset($route->file)) include $route->file;

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-07-28