项目作者: parable-php

项目描述 :
Parable Routing is a fast, intuitive url routing library.
高级语言: PHP
项目地址: git://github.com/parable-php/routing.git
创建时间: 2019-03-08T11:54:35Z
项目社区:https://github.com/parable-php/routing

开源协议:MIT License

下载


Parable Routing

Workflow Status
Latest Stable Version
Latest Unstable Version
License

Parable Routing is a fast, intuitive url routing library.

Install

Php 8.0+ and composer are required.

  1. $ composer require parable-php/routing

Usage

  1. $router = new Router();
  2. $route1 = new Route(
  3. ['GET'],
  4. 'simple-route',
  5. 'route/simple',
  6. [Controller::class, 'actionName']
  7. );
  8. $route2 = new Route(
  9. ['GET', 'POST'],
  10. 'param-route',
  11. 'route/{param}/hello',
  12. function (string $param) {
  13. echo 'Hello, ' . $username . '!';
  14. }
  15. );
  16. $router->addRoutes($route1, $route2);
  17. $match = $router->match('GET', 'route/devvoh/hello');
  18. echo $match->getName();

This would echo param-route.

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

  1. $callable = $match->getCallable();
  2. $callable(...$match->getParameterValues()->getAll());

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

  1. $route = new Route(
  2. ['GET'],
  3. 'simple-route',
  4. 'route/simple',
  5. [Controller::class, 'actionName'],
  6. [
  7. 'metadata' => 'enabled'
  8. ]
  9. );
  10. $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.

License

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