PHP code example of brokencube / circuit

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

$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
};

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;        
    }
}

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

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

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

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

$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');