Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/parable-php/routing

Parable Routing is a fast, intuitive url routing library.
https://github.com/parable-php/routing

library parable php8 routes routing url

Last synced: 2 months ago
JSON representation

Parable Routing is a fast, intuitive url routing library.

Awesome Lists containing this project

README

        

# Parable Routing

[![Workflow Status](https://github.com/parable-php/routing/workflows/Tests/badge.svg)](https://github.com/parable-php/routing/actions?query=workflow%3ATests)
[![Latest Stable Version](https://poser.pugx.org/parable-php/routing/v/stable)](https://packagist.org/packages/parable-php/routing)
[![Latest Unstable Version](https://poser.pugx.org/parable-php/routing/v/unstable)](https://packagist.org/packages/parable-php/routing)
[![License](https://poser.pugx.org/parable-php/routing/license)](https://packagist.org/packages/parable-php/routing)

Parable Routing is a fast, intuitive url routing library.

## Install

Php 8.0+ and [composer](https://getcomposer.org) are required.

```bash
$ composer require parable-php/routing
```

## Usage

```php
$router = new Router();

$route1 = new Route(
['GET'],
'simple-route',
'route/simple',
[Controller::class, 'actionName']
);

$route2 = new Route(
['GET', 'POST'],
'param-route',
'route/{param}/hello',
function (string $param) {
echo 'Hello, ' . $username . '!';
}
);

$router->addRoutes($route1, $route2);

$match = $router->match('GET', 'route/devvoh/hello');

echo $match->getName();
```

This would echo `param-route`.

Routing does not provide a direct way of executing a route, but it's easy enough:

```php
$callable = $match->getCallable();
$callable(...$match->getParameterValues()->getAll());
```

For more free-form metadata you want to attach to a `Route`, you can use metadata:

```php
$route = new Route(
['GET'],
'simple-route',
'route/simple',
[Controller::class, 'actionName'],
[
'metadata' => 'enabled'
]
);

$route->getMetadataValue('metadata'); // returns 'enabled'
```

You can use this to add template paths to a route, whether it should be visible to admins only, etc.

## Contributing

Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at [devvoh.com](https://devvoh.com).

## License

All Parable components are open-source software, licensed under the MIT license.