定制 kreme201/file-base-router 二次开发

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

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

kreme201/file-base-router

最新稳定版本:0.1.0

Composer 安装命令:

composer require kreme201/file-base-router

包简介

File based router for PHP

README 文档

README

The File Based Router library is designed to handle dynamic routing based on files.

Install

composer require kreme201/file-base-router

It has been developed and tested on PHP 8.3.

Apache Setting

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]

Usage

Project Structure

.
├── pages
│   ├── board
│   │   ├── [id].php
│   │   └── index.php
│   ├── index.php
├── public
│   └── index.php
└── vendor

public/index.php

<?php
require '/path/to/vendor/autoload.php';

$dispatcher = new Kreme201\FileBaseRouter\Dispatcher(dirname(__DIR__) . '/pages');
$template = $dispatcher->dispatch(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

if (false !== $template && file_exists($template)) {
    include $template;
} else {
    http_response_code(404);
}

When creating an instance of the Dispatcher, routing is handled based on the provided path.
Dynamic paths can be processed using [{name}] and [...{name}] to capture dynamic data.
Each data can be accessed via $_GET, and data defined with [...{name}] includes all sub-paths.

/: {base_path}/index.php
/board: {base_path}/board/index.php
/board/123: {base_path}/board/[id].php, $_GET['id'] => 123
/test/slug/example: {base_path}/test/[...slug].php, $_GET['slug'] => 'slug/example'

Example

File Base Router Example

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2024-02-28