pklink/file-router

将目录中的文件映射到路由的库

1.0.0 2016-01-03 18:30 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:47:58 UTC


README

Build Status Dependency Status

将目录中的文件映射到路由,例如 hello/world

使用方法

PHP文件的路由器

我们有以下文件结构

.
├── example.php
└── includes
	├── hello
	│   └── world.php
    └── hello.php

这里是我们 example.php

// the source path for including files
$sourcePath = new SplFileInfo(__DIR__ . '/includes');

// create router
$router = new \FileRouter\Router\IncludeRouter($sourcePath);

现在您可以在 includes 目录中加载/包含文件

$router->handleRoute('hello'); // include includes/hello.php
$router->handleRoute('hello/world'); // include includes/hello/world.php

或者像这样

$router->handleRoute($_GET['r']);

打印txt文件的路由器

我们有以下文件结构

.
├── example.php
└── docs
	├── hello
	│   └── world.txt
    └── hello.txt

这里是我们 example.php

// the source path for including files
$sourcePath = new SplFileInfo(__DIR__ . '/docs');

// create router
$router = new \FileRouter\Router\OutputTxtRouter($sourcePath);

现在您可以在 docs 目录中打印/输出文件

$router->handleRoute('hello'); // print includes/hello.txt
$router->handleRoute('hello/world'); // print includes/hello/world.txt

高级使用

编写自己的路由器

编写并添加自己的路由器没有问题。实现接口 \FileRouter\Router 或使用 \FileRouter\Router\AbstractImpl 的抽象实现,这样您只需实现 Router::handleRoute()

class CustomRouter extends \FileRouter\Router\AbstractRouter
{

	public function handleRoute($router)
	{
		/* @var \SplFileInfo $routingFile */
		$routingFile = $this->getFileByRoute($route);
		
		// do something
	}

}	

运行测试

您可以从 vendor 文件夹中使用 PHPUnit。

php composer.phar install --dev
php vendor/bin/phpunit tests/

许可协议

本软件包受 MIT 许可协议的许可。有关详细信息,请参阅 LICENSE 文件。