Skip to content
This repository was archived by the owner on Jun 9, 2022. It is now read-only.
/circuitPublic archive

Circuit: FastRouter + Middleware with HTTP Foundation

License

Notifications You must be signed in to change notification settings

brokencube/circuit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

71 Commits

Repository files navigation

circuit

Latest Stable VersionCode Climate

Router + Middleware built on top of HTTP Foundation and FastRoute

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 = newPSR6(); // PSR6 or 16 cache - can be null$log = newPSR3(); // 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 must implement the Circuit\Interfaces\Controller interface

useCircuit\Interfaces\Controller; usePsr\Container\ContainerInterfaceasContainer; class Home implements Controller{protected$container; publicfunction__construct(Container$container){$this->container = $container} publicfunctionindex(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/AddCookie.php

namespacemiddleware; useCircuit\Interfaces\{Middleware, Delegate}; useSymfony\Component\HttpFoundation\{Request, Response, Cookie}; class AddCookie implements Middleware{publicfunctionprocess(Request$request, Delegate$delegate) : Response{$response = $delegate->process($request); $cookie = newCookie('cookie', 'cookievalue', time() + (24 * 60 * 60)); $response->headers->setCookie($cookie); return$response} }

routes.php

Add middleware individually to a route

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

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

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

Or add middleware to a group of routes

$router->registerMiddleware('addcookie', newmiddleware\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', newmiddleware\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');

About

Circuit: FastRouter + Middleware with HTTP Foundation

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages