1. Go to this page and download the library: Download brokencube/circuit library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
brokencube / circuit example snippets
$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
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);
}
}
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;
}
}