Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/php-strict/simple-route
Simple request router
https://github.com/php-strict/simple-route
php php-library php7 route router routing
Last synced: 25 days ago
JSON representation
Simple request router
- Host: GitHub
- URL: https://github.com/php-strict/simple-route
- Owner: php-strict
- License: gpl-3.0
- Created: 2019-04-10T06:40:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-12-12T08:06:36.000Z (almost 5 years ago)
- Last Synced: 2024-10-09T19:34:02.180Z (about 1 month ago)
- Topics: php, php-library, php7, route, router, routing
- Language: PHP
- Size: 68.4 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Simple route
[![Software License][ico-license]](LICENSE.txt)
[![Build Status][ico-travis]][link-travis]
[![codecov][ico-codecov]][link-codecov]
[![Codacy Badge][ico-codacy]][link-codacy]Simple request router. All routes is a key/entry pairs.
Router looking for entry closest to key, and returns it
with remainder of searching key as parameters array (splited by slash).It can be used to delegate execution from core to standalone modules.
Each module takes remainder of searching key as parameters array
and use it by its own.Storage example:
```php
['some callback, module class, ... here'],
'/qwe' => ['some callback, module class, ... here'],
'/asd' => ['some callback, module class, ... here'],
'/qwe/rty' => ['some callback, module class, ... here'],
'/asd/fgh' => ['some callback, module class, ... here'],
];
```For path '/qwe/param1/param2' route returns second entry and parameters array (param1, param2).
## Supported storages
* array (supports callbacks)
* file (supports callbacks),
* SQLite,
* MySQL (uses main db connection from app).## Requirements
* PHP >= 7.1
## Install
Install with [Composer](http://getcomposer.org):
```bash
composer require php-strict/simple-route
```## Usage
Basic usage:
```php
use PhpStrict\SimpleRoute\Route;
use PhpStrict\SimpleRoute\ArrayStorage;$routes = [
'/' => [
'title' => 'Main page title',
'callback' => function () {
return 'Main page callback result';
},
],
'/qwe' => [
'title' => 'Page qwe title',
'callback' => function () {
return 'Page qwe callback result';
},
],
'/qwe/rty' => [
'title' => 'Page qwe/rty title',
],
'/qwe/rty/uio' => [
'title' => 'Page qwe/rty/uio title',
],
];$path = $_SERVER['PATH_INFO'] ?? $_SERVER['ORIG_PATH_INFO'];
$result = Route::find($path, new ArrayStorage($routes));
if (null === $result) {
//show error or redirect to mainpage
}/*
structure of $result for path '/qwe/param1/param2':
{
entry: {
key: '/qwe',
data: [
'title' => 'Page qwe title',
'callback' => function () {
return 'Page qwe callback result';
}
]
},
params: ['param1', 'param2']
}
*///just output
echo '
' . $result->entry->data['title'] . '
';if (isset($result->entry->data['callback'])) {
echo $result->entry->data['callback']();
}if (0 < count($result->params)) {
echo '
- ';
- ' . $param . ' ';
foreach ($result->params as $param) {
echo '
}
echo '
}
```
## Tests
To execute the test suite, you'll need [Codeception](https://codeception.com/).
```bash
vendor\bin\codecept run
```
[ico-license]: https://img.shields.io/badge/license-GPL-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/php-strict/simple-route/master.svg?style=flat-square
[link-travis]: https://travis-ci.org/php-strict/simple-route
[ico-codecov]: https://codecov.io/gh/php-strict/simple-route/branch/master/graph/badge.svg
[link-codecov]: https://codecov.io/gh/php-strict/simple-route
[ico-codacy]: https://api.codacy.com/project/badge/Grade/35b8cef91ae049d6b92bf270a119b877
[link-codacy]: https://www.codacy.com/app/php-strict/simple-route?utm_source=github.com&utm_medium=referral&utm_content=php-strict/simple-route&utm_campaign=Badge_Grade