Router + Middleware built on top of HTTP Foundation and FastRoute
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 routeGenerally:
$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 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.
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} }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');