PHPackages                             benycode/slim-annotation-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. benycode/slim-annotation-router

ActiveLibrary[HTTP &amp; Networking](/categories/http)

benycode/slim-annotation-router
===============================

Slim Annotation Router for Slim 4

v1.0.0(2y ago)05MITPHPPHP ^7.2 || ~8.0

Since Jan 30Pushed 2y agoCompare

[ Source](https://github.com/benycode/Slim-Annotation-Router)[ Packagist](https://packagist.org/packages/benycode/slim-annotation-router)[ RSS](/packages/benycode-slim-annotation-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Slim-Annotation-Router
======================

[](#slim-annotation-router)

 [ ![Latest Version on Packagist](https://camo.githubusercontent.com/c5a0dc5209dd789183e2906cc1b336f6e3e30553269ec2f6262bfda035ff5d94/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4e696363302f736c696d2d616e6e6f746174696f6e2d726f757465722e7376673f7374796c653d666c61742d737175617265) ](https://packagist.org/packages/nicc0/slim-annotation-router) [ ![Latest PHP Version](https://camo.githubusercontent.com/0bd6d77c9dbd5a9bcbdf81e4427a0dbe28e763d61e2764af2a7898da88f0593d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6e696363302f736c696d2d616e6e6f746174696f6e2d726f757465722e7376673f7374796c653d666c61742d737175617265) ](#) [ ![License](https://camo.githubusercontent.com/bafa0420d14738c5e067e291c46375c16e7ab7110645365b01e69efa7229aec6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4e696363302f536c696d2d416e6e6f746174696f6e2d526f757465722e7376673f7374796c653d666c61742d737175617265) ](https://github.com/Nicc0/Slim-Annotation-Router/blob/master/LICENSE) [ ![Build Status](https://camo.githubusercontent.com/78b8354f2479e31211f3bab085a095abd0d5626a05737f8c569ccd0d71e2f0b1/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4e696363302f536c696d2d416e6e6f746174696f6e2d526f757465722e7376673f7374796c653d666c61742d737175617265) ](https://travis-ci.org/Nicc0/slim-annotation-router) [ ![Coverages](https://camo.githubusercontent.com/dfde10eb88293d6693c9ac5758e08db4b898cddfcf555f32a5be68f991f0d69a/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6e696363302f736c696d2d616e6e6f746174696f6e2d726f757465722e7376673f7374796c653d666c61742d737175617265) ](https://codecov.io/gh/Nicc0/slim-annotation-router)

Annotation Router for [Slim 4.x](https://github.com/slimphp/Slim/tree/4.x)

Installation
------------

[](#installation)

It's recommended using [Composer](https://getcomposer.org/) to install Slim Annotation Router.

```
$ composer require "nicc0/slim-annotation-router"
```

This will install Slim Annotation Router and all required dependencies. Remember Slim 4.x requires PHP 7.1 or newer.

Usage
-----

[](#usage)

```
$factory = new DecoratedResponseFactory( new ResponseFactory(), new StreamFactory() );
$resolver = new CallableResolver();

$controllerPath = './app/controllers/';

$collector = new AnnotationRouteCollector( $factory, $resolver, $container );
$collector->setDefaultControllersPath( $controllersPath );
$collector->collectRoutes();

$app = new App( $factory, $container, $resolver, $collector );
```

Creating Routes by Annotation
-----------------------------

[](#creating-routes-by-annotation)

```
/**
 * Class ExampleController
 *
 * @RoutePrefix("/example")
 */
class ExampleController
{
    /**
     * @Route("/hello", methods={"GET"}, name="example.hello")
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function hello(): ResponseInterface
    {
        $response = new Response();
        $response->getBody()->write('Hello world!');

        return $response;
    }
}
```

By opening the url http://your\_site\_url/example/hello, you should see "Hello world!".

Adding Middlewares to contoller by Annotation
---------------------------------------------

[](#adding-middlewares-to-contoller-by-annotation)

To add Middleware to Controller or Action use `@Middleware("")` annotation, which pass the name of Middleware. It is important to know that the name we pass must be defined in the Container. Name passes as the third parameter in the `AnnotationRouteCollector` constructor. It is also important that the added Middleware must implements of `MiddlewareInterface` otherwise Middleware will not be added to the Route.

There is also possibility to add more than one Middleware to Controller or Action.

For example, we have to add AuthMiddleware to controller. Firstly we have to define AuthMiddleware in Container.

```
$container->set('authMiddleware', function() use ($container) {
    return new AuthContainer(container);
})
```

If Middleware exists in our Container, now we can use middleware annotation by adding `@Middleware("authMiddleware")` to controller.

```
/**
 * @RoutePrefix("/example")
 * @Middleware("authMiddleware")
 */
class ExampleController
{
    /**
     * @Route("/hello", methods={"GET"}, name="example.hello")
     */
    public function hello(): ResponseInterface
    {
        ...
    }
}
```

Tests
-----

[](#tests)

To execute the test suite, you'll need to install all development dependencies.

```
git clone https://github.com/Nicc0/Slim-Annotation-Router
composer install
composer test
```

License
-------

[](#license)

The Slim Annotation Router is licensed under the MIT license. See [License File](LICENSE.md) for more information.

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 89.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

831d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cb3e8d371d500a222cf7d259a737b7de87389296eb9584360f9a68c594d2a2dc?d=identicon)[benycode](/maintainers/benycode)

---

Top Contributors

[![Nicc0](https://avatars.githubusercontent.com/u/7517123?v=4)](https://github.com/Nicc0 "Nicc0 (17 commits)")[![benycode](https://avatars.githubusercontent.com/u/39025147?v=4)](https://github.com/benycode "benycode (2 commits)")

---

Tags

restrouterpsr7libraryslimBridgeannotation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/benycode-slim-annotation-router/health.svg)

```
[![Health](https://phpackages.com/badges/benycode-slim-annotation-router/health.svg)](https://phpackages.com/packages/benycode-slim-annotation-router)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
