https://github.com/jerowork/slim-route-attribute-provider
Define Slim routes by PHP8 attributes.
https://github.com/jerowork/slim-route-attribute-provider
attributes php8 router slim
Last synced: 12 months ago
JSON representation
Define Slim routes by PHP8 attributes.
- Host: GitHub
- URL: https://github.com/jerowork/slim-route-attribute-provider
- Owner: jerowork
- License: mit
- Created: 2020-12-07T17:38:13.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-06-02T17:38:12.000Z (about 1 year ago)
- Last Synced: 2025-06-03T06:54:09.002Z (about 1 year ago)
- Topics: attributes, php8, router, slim
- Language: PHP
- Homepage:
- Size: 286 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# slim-route-attribute-provider
[](https://github.com/jerowork/slim-route-attribute-provider/actions)
[](https://scrutinizer-ci.com/g/jerowork/slim-route-attribute-provider/code-structure)
[](https://scrutinizer-ci.com/g/jerowork/slim-route-attribute-provider)
[](LICENSE)
[](https://packagist.org/packages/jerowork/slim-route-attribute-provider)
[](http://www.php.net)
Define [Slim](https://www.slimframework.com) routes by PHP8 [attributes]((https://stitcher.io/blog/attributes-in-php-8)).
## Installation
Install via [Composer](https://getcomposer.org):
```bash
$ composer require jerowork/slim-route-attribute-provider
```
## Configuration
Instantiate `RouteAttributeConfigurator` somewhere close to the construction of your Slim application,
e.g. in your front controller (or ideally register in your PSR-11 container).
Basic configuration:
```php
use Jerowork\RouteAttributeProvider\RouteAttributeConfigurator;
use Jerowork\RouteAttributeProvider\Slim\SlimRouteAttributeProvider;
use Slim\Factory\AppFactory;
// Setup a (fictive) PSR-11 container and create Slim application
$container = new Container();
$app = AppFactory::createFromContainer($container);
// ...
// Setup route attribute configuration
$routeConfigurator = new RouteAttributeConfigurator(
SlimRouteAttributeProvider::createFromApp($app)
);
$routeConfigurator
->addDirectory(sprintf('%s/src/Infrastructure/Api/Http/Action', __DIR__))
->configure();
// ...
// Run Slim application
$app->run();
```
Extended configuration:
```php
use Jerowork\FileClassReflector\FileFinder\RegexIterator\RegexIteratorFileFinder;
use Jerowork\FileClassReflector\NikicParser\NikicParserClassReflectorFactory;
use Jerowork\RouteAttributeProvider\RouteAttributeConfigurator;
use Jerowork\RouteAttributeProvider\Slim\SlimRouteAttributeProvider;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
// ...
// All parts of the configurator can be replaced with a custom implementation
$routeConfigurator = new RouteAttributeConfigurator(
new SlimRouteAttributeProvider(
$app->getRouteCollector(),
$container
),
new ClassReflectorRouteLoader(
new NikicParserClassReflectorFactory(
new RegexIteratorFileFinder(),
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new NodeTraverser()
)
)
);
// Multiple directories can be defined
$routeConfigurator
->addDirectory(
sprintf('%s/src/Infrastructure/Api/Http/Action', __DIR__),
sprintf('%s/src/Other/Controller', __DIR__)
)
->configure();
// ...
```
## Usage
See [jerowork/route-attribute-provider](https://github.com/jerowork/route-attribute-provider#usage) for examples.