PHPackages                             ergy/slim-annotations-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. [Framework](/categories/framework)
4. /
5. ergy/slim-annotations-router

ActiveLibrary[Framework](/categories/framework)

ergy/slim-annotations-router
============================

Controller and annotations based router for Slim Framework V3

1.0.2(10y ago)81.5k1[1 PRs](https://github.com/RemyJeancolas/slim-annotations-router/pulls)MITPHPPHP &gt;=5.5.0

Since Sep 28Pushed 9y ago2 watchersCompare

[ Source](https://github.com/RemyJeancolas/slim-annotations-router)[ Packagist](https://packagist.org/packages/ergy/slim-annotations-router)[ RSS](/packages/ergy-slim-annotations-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/6c9b4409ee7a2036999b397de7bc0170c46fbf12e1eec059259f0ba585832b51/68747470733a2f2f706f7365722e707567782e6f72672f657267792f736c696d2d616e6e6f746174696f6e732d726f757465722f762f737461626c652e737667)](https://packagist.org/packages/ergy/slim-annotations-router) [![Total Downloads](https://camo.githubusercontent.com/1f866398bec20d3a9b36e6ccd71fae4a05ec893abbf711d2f11399bc5cf963de/68747470733a2f2f706f7365722e707567782e6f72672f657267792f736c696d2d616e6e6f746174696f6e732d726f757465722f646f776e6c6f6164732e737667)](https://packagist.org/packages/ergy/slim-annotations-router) [![Latest Unstable Version](https://camo.githubusercontent.com/86e14bba0814ad210ae2955418a09cd6bb40e774386cdcd7a2772cc0a3041fc4/68747470733a2f2f706f7365722e707567782e6f72672f657267792f736c696d2d616e6e6f746174696f6e732d726f757465722f762f756e737461626c652e737667)](https://packagist.org/packages/ergy/slim-annotations-router) [![License](https://camo.githubusercontent.com/de3e193756f8261ecf2c5a990d1ab2859e92dc845215eeffa36906cce74a3241/68747470733a2f2f706f7365722e707567782e6f72672f657267792f736c696d2d616e6e6f746174696f6e732d726f757465722f6c6963656e73652e737667)](https://packagist.org/packages/ergy/slim-annotations-router)

Slim Annotations Router
=======================

[](#slim-annotations-router)

Controller and annotations based router for Slim Framework V3

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

[](#installation)

Via [Composer](https://getcomposer.org/) :

```
composer require ergy/slim-annotations-router
```

Initialization
--------------

[](#initialization)

To initialize the annotations router, simply add this lines to your index.php file that loads your Slim application :

```
$app = new \Slim\App();
$c = $app->getContainer();
$c['router'] = function() use ($app) {
  return new \Ergy\Slim\Annotations\Router($app,
    '/path_to_controller_files/', // Path to controller files, will be scanned recursively
    '/path_to_cache_files/' // Path to annotations router cache files, must be writeable by web server, if it doesn't exist, router will attempt to create it
  );
};
```

If your application contains controllers in multiple folders, you can add them by passing an array in the second parameter to the constructor instead of a string, eg :

```
return new \Ergy\Slim\Annotations\Router($app,
  ['/path_to_controller_files_1/', '/path_to_controller_files_2/', ...],
  '/path_to_cache_files/'
);
```

Controller files
----------------

[](#controller-files)

The annotations router detects all files with names ending in "Controller.php" and all public methods with names ending in "Action". It then parses their annotations to generate routes recognized by Slim framework.

The following annotations are supported :

NameLevelTypeRequiredDescription@RoutePrefixClassstringnoThe route prefix, used for all routes in the current class@RouteMethodObjectyesThe route definitionThe **@Route** object accepts the following syntaxes :

```
// Route pattern (required), can contain regular expressions
@Route("/home")

// Route methods (optional), if not set only GET route will be generated
@Route("/home", methods={"GET","POST","PUT"})

// Route name (optional), used to generate route with Slim pathFor() method
@Route("/home", methods={"GET","POST","PUT"}, name="home")
```

Let's see an example :

```
/**
 * File /my/app/controllers/homeController.php
 * @RoutePrefix("/home")
 */
class HomeController
{
  /**
   * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello")
   */
  public function helloAction($name)
  {
    echo 'Hello '.$name.' !';
  }
}
```

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

Get Slim dependency container
-----------------------------

[](#get-slim-dependency-container)

Once your controller loaded, you might need to interact with the current HTTP request and response, to get theses objects, your controller simply has to extends the *Ergy\\Slim\\Annotations\\Controller* class.

Extending this class allows you to retrieve a reference to the Slim dependency container, so to retrieve the current request, you simply have to ask for **$this-&gt;request**.

Let's see an example with the previous class :

```
/**
 * File /my/app/controllers/homeController.php
 * @RoutePrefix("/home")
 */
class HomeController extends Ergy\Slim\Annotations\Controller
{
  /**
   * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello")
   */
  public function helloAction($name)
  {
    echo 'Hello '.$name.' !';

    // Dump the current request details
    var_dump($this->request);

    // Dump the current response details
    var_dump($this->response);
  }
}
```

Since we have a reference to the Slim dependency container, we can retrieve any object it contains, for example if you want to retrieve the applications settings, simply ask for **$this-&gt;settings**.

Routing events
--------------

[](#routing-events)

The annotations router exposes the methods *beforeExecuteRoute* and *afterExecuteRoute*.

These methods take as a parameter a *Ergy\\Slim\\Annotations\\RouteInfo* instance that gives you information about the running route.

Implementing these methods in your controller (or one of its parent classes) allow you to implement hook points before/after the actions are executed.

Example with the previous class :

```
/**
 * File /my/app/controllers/homeController.php
 * @RoutePrefix("/home")
 */
class HomeController
{
  public function beforeExecuteRoute(Ergy\Slim\Annotations\RouteInfo $route)
  {
    echo 'before ';
  }

  public function afterExecuteRoute() // Parameter Ergy\Slim\Annotations\RouteInfo is optional
  {
    echo ' after';
  }

  /**
   * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello")
   */
  public function helloAction($name)
  {
    echo 'Hello '.$name.' !';
  }
}
```

By opening the url **http://your\_site\_url/home/hello/foobar**, you should see "before Hello foobar ! after".

### Operation cancellation

[](#operation-cancellation)

Previous hooks allow you to cancel operations if needed :

- You can cancel execution of current route and *afterExecuteRoute* method by returning either *false* or an instance of *\\Psr\\Http\\Message\\ResponseInterface* in the method *beforeExecuteRoute*.
- You can cancel the call to *afterExecuteRoute* method by returning either *false* or an instance of *\\Psr\\Http\\Message\\ResponseInterface* in the current route method.

For example :

```
/**
 * File /my/app/controllers/homeController.php
 * @RoutePrefix("/home")
 */
class HomeController
{
  public function beforeExecuteRoute(Ergy\Slim\Annotations\RouteInfo $route)
  {
    echo 'before ';
  }

  public function afterExecuteRoute() // Parameter Ergy\Slim\Annotations\RouteInfo is optional
  {
    echo ' after';
  }

  /**
   * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello")
   */
  public function helloAction($name)
  {
    echo 'Hello '.$name.' !';
    return false;
  }
}
```

By opening the url **http://your\_site\_url/home/hello/foobar**, you should see "before Hello foobar !".

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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

Every ~32 days

Total

3

Last Release

3821d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/08a7b46d4fc0d860acb6124a132734a381c061c7177b0777309cad1234e04a66?d=identicon)[ergy](/maintainers/ergy)

---

Top Contributors

[![RemyJeancolas](https://avatars.githubusercontent.com/u/10399563?v=4)](https://github.com/RemyJeancolas "RemyJeancolas (13 commits)")

---

Tags

routerslimannotationsroutingcontrollerslimframework

### Embed Badge

![Health badge](/badges/ergy-slim-annotations-router/health.svg)

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

###  Alternatives

[klein/klein

A lightning fast router for PHP

2.7k1.1M31](/packages/klein-klein)[akrabat/rka-slim-controller

Dynamically instantiated controller classes for Slim Framework

4827.3k1](/packages/akrabat-rka-slim-controller)[juliangut/slim-controller

Slim Framework controller creator

2539.4k](/packages/juliangut-slim-controller)[martynbiz/slim3-controller

Provides controller functionality to Slim Framework v3. Also includes PHPUnit TestCase for testing controllers.

2814.4k1](/packages/martynbiz-slim3-controller)[juliangut/slim-routing

Slim framework routing

248.6k](/packages/juliangut-slim-routing)

PHPackages © 2026

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