Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created May 27, 2020 14:22
Show Gist options
  • Save kobus1998/99e2f5b9743916d99c6146be88deda64 to your computer and use it in GitHub Desktop.
Save kobus1998/99e2f5b9743916d99c6146be88deda64 to your computer and use it in GitHub Desktop.
aura router integrated with psr container
<?php
require __DIR__ . '/vendor/autoload.php';
use League\Container\Container;
$container = new Container();
$container->add('helper', Helper::class);
$router = new Routing($container);
$map = $router->getMap();
$map->get('home', '/home', ['Controller', 'home']);
$map->get('home2', '/', ['Controller', 'home']);
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
$router->handle($request);
<?php
class Routing extends \Aura\Router\RouterContainer
{
protected $container;
public function __construct($container)
{
parent::__construct();
$this->container = $container;
}
public function handle($request)
{
$matcher = $this->getMatcher();
$route = $matcher->match($request);
$callback = $route->handler;
if (is_array($callback)) {
$controllerName = reset($callback);
$methodName = end($callback);
$controller = new $controllerName($this->container);
return $controller->{$methodName}($request);
}
return $callback($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment