PHPackages                             crodas/dispatcher - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. crodas/dispatcher

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

crodas/dispatcher
=================

Dispatcher generator

v1.1.1(10y ago)99191[1 issues](https://github.com/crodas/Dispatcher/issues)1BSD-4-ClausePHPPHP &gt;=5.5

Since Sep 22Pushed 10y ago3 watchersCompare

[ Source](https://github.com/crodas/Dispatcher)[ Packagist](https://packagist.org/packages/crodas/dispatcher)[ RSS](/packages/crodas-dispatcher/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (6)Versions (36)Used By (1)

Dispatcher
==========

[](#dispatcher)

`Dispatcher` is a routing library that maps URLs to actions.

Installing
----------

[](#installing)

You can install using composer:

```
composer require crodas/dispatcher:"^1.0"
```

Defining routes
---------------

[](#defining-routes)

Routes are defined using annotations for methods or functions.

```
use Symfony\Component\HttpFoundation\Request;

/**
 * A single function can have multiple routes.
 *
 * @Route("/{user}")
 * @Route("/user/{user}")
 */
function handler(Request $req, $user)
{
  return "Hello {$user}";
}

/**
 *  You can also set default values and use multiple routes
 *
 *  @Route("/foo/{bar}")
 *  @Route("/foo", {bar="index"})
 */
function do_something_with_bar(Request $request, $bar) {
  print "I've got bar=" . $bar;
}

/**
 *  If @Route is define at a class it would behave as a
 *  namespace of prefix for the routes defined at their
 *  methods
 *
 *  @Route("/admin")
 */
class Foobar
{
  /**
   *  This handles POST /admin/login
   *
   *  @Route("/login")
   *  @Method POST
   */
  public function do_login(Request $req)
  {
  }

  /**
   *  This handles GET /admin/login
   *
   *  @Route("/login")
   */
  public function login(Request $req)
  {
  }
}
```

`Dispatcher` walks over the filesystem looking for `@Route` annotations on functions and methods.

How to use it
-------------

[](#how-to-use-it)

```
$router = new Dispatcher\Router;
$router->
  // where our controllers are located
  ->addDirectory(__DIR__ . "/../projects");

// Do the router
$router->doRoute();
```

By default, it runs in `production mode` and it won't rebuild the routes on changes. If you wish to run on development mode, you should do this:

```
$router->development();
```

Filters
-------

[](#filters)

To simply things, `Dispatcher` doesn't allow you *yet* to define regular expressions to validate your placeholders. Instead it lets you to define functions which validates and modified the values of your placeholders.

```
/**
 * In this URL, the placeholder user have a Filter, that means
 * if the controller is called we can be sure we get a valid
 * user object.
 *
 * @Route("/profile/{user}")
 */
function show_profile(Request $req, $user) {
  return "Hi {$user->name}";
}

/**
 * Validate {user} placeholders
 *
 * Check if the user exists in the database, if it does exists
 * it will return true and the controller will be called.
 *
 * @Filter("user")
 */
function some_filter(Request $req, $name, $value) {
  $userobj = DB::getUserById($value);
  if ($userobj) {
    /* I'm overriding the placeholder $name for an object of the Database */
    $req->attributes->set($name, $userobj);
    return true;
  }
  return false;
}
```

The filter should return a false if `$value` is not valid the router will jump to the next rule or will throw a `notfound` exception. `$name` is used because one callback can be used as multiple filters.

Expensive `Filters` can be cached with `@Cache ` annotation. The cache mechanism is defined in the application with a class which implements `FilterCache` interface.

Valid Patterns
--------------

[](#valid-patterns)

- `/foo/bar`
- `/foo/{user}`
- `/foo/{user:user1}/{user:user2}`: Inside the `Request` object the user objects will be named `user1` and `user2` to avoid name collisions.
- `/foo/{user:u1}-vs-{user:u2}.{ext}`: We can have multiple variables inside one single directory level as long as they are separated by constants.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity70

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

Recently: every ~63 days

Total

31

Last Release

3694d ago

Major Versions

v0.1.23 → v1.0.02015-06-03

PHP version history (2 changes)v0.1.17PHP &gt;=5.3

v1.0.4PHP &gt;=5.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/36463?v=4)[C](/maintainers/crodas)[@crodas](https://github.com/crodas)

---

Top Contributors

[![crodas](https://avatars.githubusercontent.com/u/36463?v=4)](https://github.com/crodas "crodas (283 commits)")

### Embed Badge

![Health badge](/badges/crodas-dispatcher/health.svg)

```
[![Health](https://phpackages.com/badges/crodas-dispatcher/health.svg)](https://phpackages.com/packages/crodas-dispatcher)
```

###  Alternatives

[illuminate/session

The Illuminate Session package.

9937.4M753](/packages/illuminate-session)[illuminate/cookie

The Illuminate Cookie package.

224.3M122](/packages/illuminate-cookie)[codefog/contao-haste

haste extension for Contao Open Source CMS

42650.8k139](/packages/codefog-contao-haste)[codefog/contao-news_categories

News Categories bundle for Contao Open Source CMS

3183.3k6](/packages/codefog-contao-news-categories)[netgen/content-browser

Netgen Content Browser is a Symfony bundle that provides an interface which selects items from any kind of backend and returns the IDs of selected items back to the calling code.

14112.1k8](/packages/netgen-content-browser)[leapt/core-bundle

Symfony LeaptCoreBundle

2529.1k4](/packages/leapt-core-bundle)

PHPackages © 2026

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