The simple URL router built with PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
.github
src
tests
.editorconfig
.gitattributes
.gitignore
LICENSE
README.md
composer.json
composer.lock
phpunit.xml

README.md

ahi-router

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads composer.lock CircleCI

The simple URL router built with PHP.

Ahiru + Router = ahi-router

Ahiru means "duck" in japanese.

Packagist - bmf-san/ahi-router

Installaion

composer require bmf-san/ahi-router

Usage

<?php
require_once("vendor/autoload.php");

use bmfsan\AhiRouter\Router;

$routes = [
    '/' => [
        'END_POINT' => [
            'GET' => 'PATH: / METHOD: GET',
        ],
        'users' => [
            'END_POINT' => [
                'GET' => 'PATH: /users METHOD: GET',
            ],
            ':user_id' => [
                'END_POINT' => [
                    'GET' => 'PATH: /users/:user_id METHOD: GET',
                    'POST' => 'PATH: /users/:user_id METHOD: POST',
                ],
                'events' =>  [
                    'END_POINT' => [
                        'GET' => 'PATH: /users/:user_id/events METHOD: GET',
                    ],
                    ':event_id' => [
                        'END_POINT' => [
                            'GET' => 'PATH: /users/:user_id/events/:event_id METHOD: GET',
                            'POST' => 'PATH: /users/:user_id/events/:event_id METHOD: POST',
                        ],
                    ],
                ],
            ],
            ':event_id' => [
                'END_POINT' => [
                    'GET' => 'PATH: /users/:event_id METHOD: GET',
                    'POST' => 'PATH: /users/:event_id METHOD: POST',
                ],
            ],
            'support' => [
                'END_POINT' => [
                    'GET' => 'PATH: /users/support METHOD: GET',
                ],
            ],
        ],
    ],
];

$currentPath = '/users/1/events/10';
$currentMethod = 'GET';
$currentParams = [
    ':user_id',
    ':event_id',
];

$router = new Router();

$currentPathArray = $router->createArrayFromCurrentPath($currentPath);
var_dump($router->search($routes, $currentPathArray, $currentMethod, $currentParams));
// array(2) {
//   'action' =>
//   string(50) "PATH: /users/:user_id/events/:event_id METHOD: GET"
//   'params' =>
//   array(2) {
//     ':user_id' =>
//     string(1) "1"
//     ':event_id' =>
//     string(2) "10"
//   }
// }

Contributing

We welcome your issue or pull request from everyone. Please check ISSUE_TEMPLATE.md and PULL_REQUEST_TEMPLATE.md to contribute.

License

This project is licensed under the terms of the MIT license.

Author

bmf - A Web Developer in Japan.