brokencube / circuit
Microframework - controller+dispatcher
Installs: 3 557
Dependents: 1
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
- nikic/fast-route: ^1.2
- psr/cache: ^1.0
- psr/container: ^1.0
- psr/log: ^1.0
- psr/simple-cache: ^1.0
- symfony/http-foundation: ^3.2
Requires (Dev)
- phpunit/phpunit: ^5.0
This package is not auto-updated.
Last update: 2022-06-22 05:46:54 UTC
README
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 = 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 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/AddCookie.php
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
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');