PHPackages                             brokencube/circuit - 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. brokencube/circuit

AbandonedArchivedLibrary[Framework](/categories/framework)

brokencube/circuit
==================

Microframework - controller+dispatcher

2.0.0(8y ago)43.7k1MITPHPPHP &gt;=7.0

Since May 9Pushed 7y ago1 watchersCompare

[ Source](https://github.com/brokencube/circuit)[ Packagist](https://packagist.org/packages/brokencube/circuit)[ RSS](/packages/brokencube-circuit/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (7)Versions (13)Used By (1)

circuit
=======

[](#circuit)

[![Latest Stable Version](https://camo.githubusercontent.com/3894fc9a959b66bad24547af2255616ef56ecc379db0a0ccec19615da4b8b3d3/68747470733a2f2f706f7365722e707567782e6f72672f62726f6b656e637562652f636972637569742f762f737461626c65)](https://packagist.org/packages/brokencube/circuit)[![Code Climate](https://camo.githubusercontent.com/37252dbb7152d27017ebc3403bb00d50049ef32c6c6821ae47032231c3343aff/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f62726f6b656e637562652f636972637569742f6261646765732f6770612e737667)](https://codeclimate.com/github/brokencube/circuit)

Router + Middleware built on top of [HTTP Foundation](https://github.com/symfony/http-foundation) and [FastRoute](https://github.com/nikic/FastRoute)

Basic Usage
===========

[](#basic-usage)

See FastRoute docs for more info on advanced matching patterns, grouping etc

```
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();  // From HTTP Foundation

$options = [];         // Options - allows you to provide alternative internals - see below
$cache = new PSR6();   // PSR6 or 16 cache - can be null
$log = new PSR3();     // PSR-3 compatible log - can be null

$router = new \Circuit\Router($options, $cache, $log);
$router->defineRoutes(function (\Circuit\RouteCollector $r) {
    $r->get('/', 'controllers\Home');                    // Calls \controllers\Home->index($request);
    $r->get('/search', 'controllers\Home@search');       // Calls \controllers\Home->search($request);
    $r->get('/blog/{id}', 'controllers\Blog@index', []); // Calls \controllers\Blog->index($request, $id);
    $r->addGroup('/group', [], function(Circuit\RouteCollector $r) {
        $r->get('/route', 'controllers\GroupRoute@index');
    }
}

$router->run($request);  // Dispatch route
```

Generally:

```
$r->get($route, $controllerName, $middlewareArray);
$r->post($route, $controllerName, $middlewareArray);
$r->addRoute(['GET', 'POST'], $route, $controllerName, $middlewareArray);
$r->addGroup($prefix, $middlewareArray, function(Circuit\RouteCollector $r) {
   // Group routes
};
```

`$controllerName` should be in format `namespaced\ControllerClass@method` (Similar to Laravel)

Controllers
===========

[](#controllers)

Controllers must implement the `Circuit\Interfaces\Controller` interface

```
use Circuit\Interfaces\Controller;
use Psr\Container\ContainerInterface as Container;

class Home implements Controller
{
    protected $container;
    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    public function index(Request $request)
    {
        return 'Home Page'; // Can also return instances of Response, or an array (will be `json_encode`d);
    }
}
```

**Note:** *Currently Circuit only supports the "Service Locator" pattern for DI. In future versions, autowiring will be introduced and the interface requirement above will be relaxed.*

Middleware
==========

[](#middleware)

### `middleware/AddCookie.php`

[](#middlewareaddcookiephp)

```
namespace middleware;

use Circuit\Interfaces\{Middleware, Delegate};
use Symfony\Component\HttpFoundation\{Request, Response, Cookie};

class AddCookie implements Middleware
{
    public function process(Request $request, Delegate $delegate) : Response
    {
        $response = $delegate->process($request);

        $cookie = new Cookie('cookie', 'cookievalue', time() + (24 * 60 * 60));
        $response->headers->setCookie($cookie);

        return $response;
    }
}
```

### `routes.php`

[](#routesphp)

Add middleware individually to a route

```
$router->defineRoutes(function (\Circuit\RouteCollector $r) {
  $r->get('/', 'controllers\Home', [new middleware\AddCookie()]);
}
```

Or register the middleware in the router, and call it by name

```
$router->registerMiddleware('addcookie', new middleware\AddCookie());
$router->defineRoutes(function (\Circuit\RouteCollector $r) {
  $r->get('/', 'controllers\Home', ['addcookie']);
}
```

Or add middleware to a group of routes

```
$router->registerMiddleware('addcookie', new middleware\AddCookie());
$router->defineRoutes(function (\Circuit\RouteCollector $r) {
  $r->addGroup('', ['addcookie'], function(Circuit\RouteCollector $r) {
    $r->get('/', 'controllers\Home');
  }
}
```

Or add middleware to be run before a route is even matched (this will logically be applied to all routes, as it happens before the matching step. This allows for middleware to modify the route before matching)

```
$router->registerMiddleware('addcookie', new middleware\AddCookie());
$router->addPrerouteMiddleware('addcookie');
$router->defineRoutes(function (\Circuit\RouteCollector $r) {
  $r->get('/', 'controllers\Home');
}
```

Middleware will be run in the order defined, with preroute middleware always running first, e.g.:

```
$router->addPrerouteMiddleware('middleware1');
$router->defineRoutes(function (\Circuit\RouteCollector $r) {
  $r->addGroup('', ['middleware3', 'middleware4'], function(Circuit\RouteCollector $r) {
    $r->get('/', 'controllers\Home', ['middleware5']);
  }
}
$router->addPrerouteMiddleware('middleware2');
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity65

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 ~25 days

Recently: every ~16 days

Total

11

Last Release

3038d ago

Major Versions

1.2.5 → 2.0.02018-01-19

### Community

Maintainers

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

---

Top Contributors

[![brokencube](https://avatars.githubusercontent.com/u/3684869?v=4)](https://github.com/brokencube "brokencube (6 commits)")

---

Tags

fastroutehttp-foundationmiddlewareroutermvccontrollerdispatcher

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brokencube-circuit/health.svg)

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

###  Alternatives

[laravel/framework

The Laravel Framework.

34.6k509.9M17.0k](/packages/laravel-framework)[symfony/symfony

The Symfony PHP framework

31.3k86.3M2.2k](/packages/symfony-symfony)[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)

PHPackages © 2026

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